home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / dired / ange-ftp.el next >
Encoding:
Text File  |  1995-07-19  |  224.4 KB  |  6,059 lines

  1. ;;; ange-ftp.el --- transparent FTP support for GNU Emacs
  2. ;; Keywords: comm
  3.  
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5. ;;
  6. ;; File:         ange-ftp.el
  7. ;; RCS:          Header: ange-ftp.el,v 4.20 92/08/14 17:04:34 ange Exp
  8. ;; Description:  transparent FTP support for GNU Emacs
  9. ;; Author:       Andy Norman, ange@hplb.hpl.hp.com
  10. ;; Created:      Thu Oct 12 14:00:05 1989
  11. ;; Modified:     Wed May  3 00:50:40 1995 (Andy Norman) ange@hplb.hpl.hp.com
  12. ;; Modified for XEmacs by jwz
  13. ;;
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15.  
  16. ;;; Copyright (C) 1989, 1990, 1991, 1992  Andy Norman.
  17. ;;;
  18. ;;; Author: Andy Norman (ange@hplb.hpl.hp.com)
  19. ;;;
  20. ;;; This program is free software; you can redistribute it and/or modify
  21. ;;; it under the terms of the GNU General Public License as published by
  22. ;;; the Free Software Foundation; either version 1, or (at your option)
  23. ;;; any later version.
  24. ;;;
  25. ;;; This program is distributed in the hope that it will be useful,
  26. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. ;;; GNU General Public License for more details.
  29. ;;;
  30. ;;; A copy of the GNU General Public License can be obtained from this
  31. ;;; program's author (send electronic mail to ange@hplb.hpl.hp.com) or from
  32. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  33. ;;; 02139, USA.
  34.  
  35. ;;; Description:
  36. ;;;
  37. ;;; This package attempts to make accessing files and directories using FTP
  38. ;;; from within GNU Emacs as simple and transparent as possible.  A subset of
  39. ;;; the common file-handling routines are extended to interact with FTP.
  40.  
  41. ;;; Installation:
  42. ;;;
  43. ;;; Byte-compile ange-ftp.el to ange-ftp.elc and put them both in a directory
  44. ;;; on your load-path.  Load the package from your .emacs file with:
  45. ;;;
  46. ;;;   (require 'ange-ftp).
  47. ;;;
  48. ;;; ange-ftp can't sensibly be auto-loaded; you are either using it, or you
  49. ;;; ain't.
  50.  
  51. ;;; Usage:
  52. ;;;
  53. ;;; Some of the common GNU Emacs file-handling operations have been made
  54. ;;; FTP-smart.  If one of these routines is given a filename that matches
  55. ;;; '/user@host:path' then it will spawn an FTP process connecting to machine
  56. ;;; 'host' as account 'user' and perform its operation on the file 'path'.
  57. ;;;
  58. ;;; For example: if find-file is given a filename of:
  59. ;;;
  60. ;;;   /ange@anorman:/tmp/notes
  61. ;;;
  62. ;;; then ange-ftp will spawn an FTP process, connect to the host 'anorman' as
  63. ;;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
  64. ;;; contents of that file as if it were on the local filesystem.  If ange-ftp
  65. ;;; needed a password to connect then it would prompt the user in the
  66. ;;; minibuffer.
  67.  
  68. ;;; Extended filename syntax:
  69. ;;;
  70. ;;; The default extended filename syntax is '/user@host:path', where the
  71. ;;; 'user@' part may be omitted.  This syntax can be customised to a certain
  72. ;;; extent by changing ange-ftp-path-format.  There are limitations.
  73. ;;;
  74. ;;; If the user part is omitted then ange-ftp will generate a default user
  75. ;;; instead whose value depends on the variable ange-ftp-default-user.
  76.  
  77. ;;; Passwords:
  78. ;;;
  79. ;;; A password is required for each host / user pair.  This will be prompted
  80. ;;; for when needed, unless already set by calling ange-ftp-set-passwd, or
  81. ;;; specified in a *valid* ~/.netrc file.
  82.  
  83. ;;; Passwords for user "anonymous":
  84. ;;;
  85. ;;; Passwords for the user "anonymous" (or "ftp") are handled specially.  The
  86. ;;; variable ange-ftp-generate-anonymous-password controls what happens: if
  87. ;;; the value of this variable is a string, then this is used as the password;
  88. ;;; if non-nil, then a password is created from the name of the user and the
  89. ;;; hostname of the machine on which GNU Emacs is running; if nil (the
  90. ;;; default) then the user is prompted for a password as normal.
  91.  
  92. ;;; "Dumb" UNIX hosts:
  93. ;;;
  94. ;;; The FTP servers on some UNIX machines have problems if the 'ls' command is
  95. ;;; used.
  96. ;;;
  97. ;;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
  98. ;;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
  99. ;;; that this change will take effect for the current GNU Emacs session only.
  100. ;;; See below for a discussion of non-UNIX hosts.  If a large number of
  101. ;;; machines with similar hostnames have this problem then it is easier to set
  102. ;;; the value of ange-ftp-dumb-unix-host-regexp in your .emacs file. ange-ftp
  103. ;;; is unable to automatically recognize dumb unix hosts.
  104.  
  105. ;;; File name completion:
  106. ;;;
  107. ;;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
  108. ;;; To do filename completion, ange-ftp needs a listing from the remote host.
  109. ;;; Therefore, for very slow connections, it might not save any time.
  110.  
  111. ;;; FTP processes:
  112. ;;;
  113. ;;; When ange-ftp starts up an FTP process, it leaves it running for speed
  114. ;;; purposes.  Some FTP servers will close the connection after a period of
  115. ;;; time, but ange-ftp should be able to quietly reconnect the next time that
  116. ;;; the process is needed.
  117. ;;;
  118. ;;; The FTP process will be killed should the associated "*ftp user@host*"
  119. ;;; buffer be deleted.  This should not cause ange-ftp any grief.
  120.  
  121. ;;; Binary file transfers:
  122. ;;;
  123. ;;; By default ange-ftp will transfer files in ASCII mode.  If a file being
  124. ;;; transferred matches the value of ange-ftp-binary-file-name-regexp then the
  125. ;;; FTP process will be toggled into BINARY mode before the transfer and back
  126. ;;; to ASCII mode after the transfer.
  127.  
  128. ;;; Account passwords:
  129. ;;;
  130. ;;; Some FTP servers require an additional password which is sent by the
  131. ;;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
  132. ;;; specify an account password by either calling ange-ftp-set-account, or by
  133. ;;; specifying an account token in the .netrc file.  If the account password
  134. ;;; is set by either of these methods then ange-ftp will issue an ACCOUNT
  135. ;;; command upon starting the FTP process.
  136.  
  137. ;;; Preloading:
  138. ;;;
  139. ;;; ange-ftp can be preloaded, but must be put in the site-init.el file and
  140. ;;; not the site-load.el file in order for the documentation strings for the
  141. ;;; functions being overloaded to be available.
  142.  
  143. ;;; Status reports:
  144. ;;;
  145. ;;; Most ange-ftp commands that talk to the FTP process output a status
  146. ;;; message on what they are doing.  In addition, ange-ftp can take advantage
  147. ;;; of the FTP client's HASH command to display the status of transferring
  148. ;;; files and listing directories.  See the documentation for the variables
  149. ;;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
  150. ;;; ange-ftp-process-verbose for more details.
  151.  
  152. ;;; Gateways:
  153. ;;;
  154. ;;; Sometimes it is neccessary for the FTP process to be run on a different
  155. ;;; machine than the machine running GNU Emacs.  This can happen when the
  156. ;;; local machine has restrictions on what hosts it can access.
  157. ;;;
  158. ;;; ange-ftp has support for running the ftp process on a different (gateway)
  159. ;;; machine.  The way it works is as follows:
  160. ;;;
  161. ;;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
  162. ;;;    that doesn't have the access restrictions.
  163. ;;;
  164. ;;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
  165. ;;;    that matches hosts that can be contacted from running a local ftp
  166. ;;;    process, but fails to match hosts that can't be accessed locally.  For
  167. ;;;    example:
  168. ;;;
  169. ;;;    "\\.hp\\.com$\\|^[^.]*$"
  170. ;;;
  171. ;;;    will match all hosts that are in the .hp.com domain, or don't have an
  172. ;;;    explicit domain in their name, but will fail to match hosts with
  173. ;;;    explicit domains or that are specified by their ip address.
  174. ;;;
  175. ;;; 3) Using NFS and symlinks, make sure that there is a shared directory with
  176. ;;;    the *same* name between the local machine and the gateway machine.
  177. ;;;    This directory is neccessary for temporary files created by ange-ftp.
  178. ;;;
  179. ;;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
  180. ;;;    this directory plus an identifying filename prefix.  For example:
  181. ;;;
  182. ;;;    "/nfs/hplose/ange/ange-ftp"
  183. ;;;
  184. ;;;    where /nfs/hplose/ange is a directory that is shared between the
  185. ;;;    gateway machine and the local machine.
  186. ;;;
  187. ;;; The simplest way of getting a ftp process running on the gateway machine
  188. ;;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
  189. ;;; can't do this for some reason such as security then points 7 onwards will
  190. ;;; discuss an alternative approach.
  191. ;;;
  192. ;;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
  193. ;;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
  194. ;;;
  195. ;;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
  196. ;;;    isn't already.  This tells ange-ftp that you are using a remote shell
  197. ;;;    rather than logging in using telnet or rlogin.
  198. ;;;
  199. ;;; That should be all you need to allow ange-ftp to spawn a ftp process on
  200. ;;; the gateway machine.  If you have to use telnet or rlogin to get to the
  201. ;;; gateway machine then follow the instructions below.
  202. ;;;
  203. ;;; 7) Set the variable ange-ftp-gateway-program to the name of the program
  204. ;;;    that lets you log onto the gateway machine.  This may be something like
  205. ;;;    telnet or rlogin.
  206. ;;;
  207. ;;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
  208. ;;;    expression that matches the prompt you get when you login to the
  209. ;;;    gateway machine.  Be very specific here; this regexp must not match
  210. ;;;    *anything* in your login banner except this prompt.
  211. ;;;    shell-prompt-pattern is far too general as it appears to match some
  212. ;;;    login banners from Sun machines.  For example:
  213. ;;;
  214. ;;;    "^$*$ *"
  215. ;;;
  216. ;;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
  217. ;;;    ange-ftp know that it has to "hand-hold" the login to the gateway
  218. ;;;    machine.
  219. ;;;
  220. ;;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
  221. ;;;     that will put the pty connected to the gateway machine into a
  222. ;;;     no-echoing mode, and will strip off carriage-returns from output from
  223. ;;;     the gateway machine.  For example:
  224. ;;;
  225. ;;;     "stty -onlcr -echo"
  226. ;;;
  227. ;;;     will work on HP-UX machines, whereas:
  228. ;;;
  229. ;;;     "stty -echo nl"
  230. ;;;
  231. ;;;     appears to work for some Sun machines.
  232. ;;;
  233. ;;; That's all there is to it.
  234.  
  235. ;;; Smart gateways:
  236. ;;;
  237. ;;; If you have a "smart" ftp program that allows you to issue commands like
  238. ;;; "USER foo@bar" which do nice proxy things, then look at the variables
  239. ;;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
  240.  
  241. ;;; Tips for using ange-ftp:
  242. ;;;
  243. ;;; 1. For dired to work on a host which marks symlinks with a trailing @ in
  244. ;;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
  245. ;;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
  246. ;;;    there is a chance you might connect to an ULTRIX machine (such as
  247. ;;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
  248. ;;;    the side effect that dired will have problems with symlinks whose names
  249. ;;;    end in an @. If you get youself into this situation then editing
  250. ;;;    dired's ls-switches to remove "F", will temporarily fix things.
  251. ;;;
  252. ;;; 2. If you know that you are connecting to a certain non-UNIX machine
  253. ;;;    frequently, and ange-ftp seems to be unable to guess its host-type,
  254. ;;;    then setting the appropriate host-type regexp
  255. ;;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
  256. ;;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
  257. ;;;    ange-ftp's inability to recognize the host-type as a bug.
  258. ;;;
  259. ;;; 3. For slow connections, you might get "listing unreadable" error
  260. ;;;    messages, or get an empty buffer for a file that you know has something
  261. ;;;    in it. The solution is to increase the value of ange-ftp-retry-time.
  262. ;;;    Its default value is 5 which is plenty for reasonable connections.
  263. ;;;    However, for some transatlantic connections I set this to 20.
  264. ;;;
  265. ;;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
  266. ;;;    copying the file to the local machine, compressing it there, and then
  267. ;;;    sending it back. Binary file transfers between machines of different
  268. ;;;    architectures can be a risky business. Test things out first on some
  269. ;;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
  270. ;;;    moving them through the local machine. Again, be careful when doing
  271. ;;;    this with binary files on non-Unix machines.
  272. ;;;
  273. ;;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
  274. ;;;    (list of dired commands for which confirmation is not asked).  You
  275. ;;;    might want to reconsider your setting of this variable, because you
  276. ;;;    might want confirmation for more commands on remote direds than on
  277. ;;;    local direds. For example, I strongly recommend that you not include
  278. ;;;    compress and uncompress in this list. If there is enough demand it
  279. ;;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
  280. ;;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
  281. ;;;    is a list of commands for which confirmation would be suppressed.  Then
  282. ;;;    remote dired listings would take their (buffer-local) value of
  283. ;;;    dired-no-confirm from this alist. Who votes for this?
  284.  
  285. ;;; ---------------------------------------------------------------------
  286. ;;; Non-UNIX support:
  287. ;;; ---------------------------------------------------------------------
  288.  
  289. ;;; VMS support:
  290. ;;;
  291. ;;; Ange-ftp has full support for VMS hosts, including tree dired support.  It
  292. ;;; should be able to automatically recognize any VMS machine. However, if it
  293. ;;; fails to do this, you can use the command ange-ftp-add-vms-host.  As well,
  294. ;;; you can set the variable ange-ftp-vms-host-regexp in your .emacs file. We
  295. ;;; would be grateful if you would report any failures to automatically
  296. ;;; recognize a VMS host as a bug.
  297. ;;;
  298. ;;; Filename Syntax:
  299. ;;;
  300. ;;; For ease of *implementation*, the user enters the VMS filename syntax in a
  301. ;;; UNIX-y way.  For example:
  302. ;;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
  303. ;;; would be entered as:
  304. ;;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
  305. ;;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
  306. ;;;  [.CSV.POLICY]RULES.MEM
  307. ;;; you would type:
  308. ;;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
  309. ;;;
  310. ;;; A legal VMS filename is of the form: FILE.TYPE;##
  311. ;;; where FILE can be up to 39 characters
  312. ;;;       TYPE can be up to 39 characters
  313. ;;;       ## is a version number (an integer between 1 and 32,767)
  314. ;;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
  315. ;;; $ cannot begin a filename, and - cannot be used as the first or last
  316. ;;; character.
  317. ;;;
  318. ;;; Tips:
  319. ;;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
  320. ;;;    Therefore, to access a VMS file, you must enter the filename with upper
  321. ;;;    case letters.
  322. ;;; 2. To access the latest version of file under VMS, you use the filename
  323. ;;;    without the ";" and version number. You should always edit the latest
  324. ;;;    version of a file. If you want to edit an earlier version, copy it to a
  325. ;;;    new file first. This has nothing to do with ange-ftp, but is simply
  326. ;;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
  327. ;;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
  328. ;;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
  329. ;;;    that VMS will not allow you to save the file because it will refuse to
  330. ;;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
  331. ;;;    attach the buffer to this file. To get out of this situation, M-x
  332. ;;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
  333. ;;;    latest version of the file. For this reason, in tree dired "f"
  334. ;;;    (dired-find-file), always loads the file sans version, whereas "v",
  335. ;;;    (dired-view-file), always loads the explicit version number. The
  336. ;;;    reasoning being that it reasonable to view old versions of a file, but
  337. ;;;    not to edit them.
  338. ;;; 3. EMACS has a feature in which it does environment variable substitution
  339. ;;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
  340. ;;;    by typing $$. There is a bug in EMACS, in that it neglects to quote the
  341. ;;;    $'s in the default directory when it writes it in the minibuffer.  You
  342. ;;;    must edit the minibuffer to quote the $'s manually. Hopefully, this bug
  343. ;;;    will be fixed in EMACS 19. If you use Sebastian Kremer's gmhist (V 4.26
  344. ;;;    or newer), you will not have this problem.
  345.  
  346. ;;; MTS support:
  347. ;;;
  348. ;;; Ange-ftp has full support, including tree dired support, for hosts running
  349. ;;; the Michigan terminal system.  It should be able to automatically
  350. ;;; recognize any MTS machine. However, if it fails to do this, you can use
  351. ;;; the command ange-ftp-add-mts-host.  As well, you can set the variable
  352. ;;; ange-ftp-mts-host-regexp in your .emacs file. We would be grateful if you
  353. ;;; would report any failures to automatically recognize a MTS host as a bug.
  354. ;;;
  355. ;;; Filename syntax:
  356. ;;; 
  357. ;;; MTS filenames are entered in a UNIX-y way. For example, if your account
  358. ;;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
  359. ;;; entered as
  360. ;;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
  361. ;;; In other words, MTS accounts are treated as UNIX directories. Of course,
  362. ;;; to access a file in another account, you must have access permission for
  363. ;;; it.  If FILE were in your own account, then you could enter it in a
  364. ;;; relative path fashion as
  365. ;;;   /YYYY@mtsg.ubc.ca:FILE
  366. ;;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
  367. ;;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
  368. ;;; like.) MTS filenames are always in upper case, and hence be sure to enter
  369. ;;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
  370. ;;; is.
  371.  
  372. ;;; CMS support:
  373. ;;; 
  374. ;;; Ange-ftp has full support, including tree dired support, for hosts running
  375. ;;; CMS.  It should be able to automatically recognize any CMS machine.
  376. ;;; However, if it fails to do this, you can use the command
  377. ;;; ange-ftp-add-cms-host.  As well, you can set the variable
  378. ;;; ange-ftp-cms-host-regexp in your .emacs file. We would be grateful if you
  379. ;;; would report any failures to automatically recognize a CMS host as a bug.
  380. ;;; 
  381. ;;; Filename syntax:
  382. ;;;
  383. ;;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
  384. ;;; treated as UNIX directories. For example to access the file READ.ME in
  385. ;;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
  386. ;;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
  387. ;;; If *.301 is the default minidisk for this account, you could access
  388. ;;; FOO.BAR on this minidisk as
  389. ;;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
  390. ;;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
  391. ;;; up to 8 characters. Again, beware that CMS filenames are always upper
  392. ;;; case, and hence must be entered as such.
  393. ;;;
  394. ;;; Tips:
  395. ;;; 1. CMS machines, with the exception of anonymous accounts, nearly always
  396. ;;;    need an account password. To have ange-ftp send an account password,
  397. ;;;    you can either include it in your .netrc file, or use
  398. ;;;    ange-ftp-set-account.
  399. ;;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
  400. ;;;    can fix this.
  401. ;;;
  402. ;;; ------------------------------------------------------------------
  403. ;;; Bugs:
  404. ;;; ------------------------------------------------------------------
  405. ;;; 
  406. ;;; 1. Umask problems:
  407. ;;;    Be warned that files created by using ange-ftp will take account of the
  408. ;;;    umask of the ftp daemon process rather than the umask of the creating
  409. ;;;    user.  This is particulary important when logging in as the root user.
  410. ;;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
  411. ;;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
  412. ;;;    suspect that there is something similar on other systems.
  413. ;;;
  414. ;;; 2. Some combinations of FTP clients and servers break and get out of sync
  415. ;;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
  416. ;;;    machines cause this problem for some FTP clients. Using
  417. ;;;    ange-ftp-kill-process can be used to restart the ftp process, which
  418. ;;;    should get things back in synch.
  419. ;;;
  420. ;;; 3. Ange-ftp does not check to make sure that when creating a new file,
  421. ;;;    you provide a valid filename for the remote operating system.
  422. ;;;    If you do not, then the remote FTP server will most likely
  423. ;;;    translate your filename in some way. This may cause ange-ftp to
  424. ;;;    get confused about what exactly is the name of the file. The
  425. ;;;    most common causes of this are using lower case filenames on systems
  426. ;;;    which support only upper case, and using filenames which are too
  427. ;;;    long.
  428. ;;;
  429. ;;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
  430. ;;;
  431. ;;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
  432. ;;;    for some reason creates a FTP process that only talks via pipes then
  433. ;;;    ange-ftp won't be getting the information it requires at the time that
  434. ;;;    it wants it since pipes flush at different times to pty's.  One
  435. ;;;    disgusting way around this problem is to talk to the FTP process via
  436. ;;;    rlogin which does the 'right' things with pty's.
  437. ;;;
  438. ;;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
  439. ;;;    worried about this too much. Eventually, we should have some caching
  440. ;;;    of the current minidisk.
  441. ;;;    
  442. ;;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
  443. ;;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
  444. ;;;    to it. This is (understandably) beyond ange-ftp.
  445. ;;;
  446. ;;; 8. Remote to remote copying of files on non-Unix machines can be risky.
  447. ;;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
  448. ;;;    will use binary mode for the copy. Between systems of different
  449. ;;;    architecture, this still may not be enough to guarantee the integrity
  450. ;;;    of binary files. Binary file transfers from VMS machines are
  451. ;;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
  452. ;;;    an alist of OS type, regexp pairs?
  453. ;;;
  454. ;;; 9. The code to do compression of files over ftp is not as careful as it
  455. ;;;    should be. It deletes the old remote version of the file, before
  456. ;;;    actually checking if the local to remote transfer of the compressed
  457. ;;;    file succeeds. Of course to delete the original version of the file
  458. ;;;    after transferring the compressed version back is also dangerous,
  459. ;;;    because some OS's have severe restrictions on the length of filenames,
  460. ;;;    and when the compressed version is copied back the "-Z" or ".Z" may be
  461. ;;;    truncated. Then, ange-ftp would delete the only remaining version of
  462. ;;;    the file.  Maybe ange-ftp should make backups when it compresses files
  463. ;;;    (of course, the backup "~" could also be truncated off, sigh...).
  464. ;;;    Suggestions?
  465. ;;;
  466.  
  467. ;;; 10. If a dir listing is attempted for an empty directory on (at least
  468. ;;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
  469. ;;;     I don't know how to get ange-ftp work to around it.
  470. ;;;
  471. ;;; 11. Bombs on filenames that start with a space. Deals well with filenames
  472. ;;;     containing spaces, but beware that the remote ftpd may not like them
  473. ;;;     much.
  474. ;;;
  475. ;;; 12. No classic dired support for non-UNIX systems. Tree dired was enough.
  476. ;;; 
  477. ;;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
  478. ;;;     with a trailing @ in a ls -alF listing. In order to account for this
  479. ;;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
  480. ;;;     parsing a listing with the F switch. This will cause ange-ftp to
  481. ;;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
  482. ;;;     ends in an @. ange-ftp will correct itself if you take F out of the
  483. ;;;     dired ls switches (C-u s will allow you to edit the switches). The
  484. ;;;     dired buffer will be automatically reverted, which will allow ange-ftp
  485. ;;;     to fix its files hashtable.  A cookie to anyone who can think of a
  486. ;;;     fast, sure-fire way to recognize ULTRIX over ftp.
  487.  
  488. ;;; If you find any bugs or problems with this package, PLEASE either e-mail
  489. ;;; the above author, or send a message to the ange-ftp-lovers mailing list
  490. ;;; below.  Ideas and constructive comments are especially welcome.
  491.  
  492. ;;; ange-ftp-lovers:
  493. ;;;
  494. ;;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
  495. ;;; users of ange-ftp are welcome to subscribe (see below) and to discuss
  496. ;;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
  497. ;;; the mailing list.
  498. ;;;
  499. ;;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
  500. ;;; list, please mail one of the following addresses:
  501. ;;;
  502. ;;;     ange-ftp-lovers-request@anorman.hpl.hp.com
  503. ;;; or
  504. ;;;     ange-ftp-lovers-request%anorman.hpl.hp.com@hplb.hpl.hp.com
  505. ;;;
  506. ;;; Please don't forget the -request part.
  507. ;;;
  508. ;;; For mail to be posted directly to ange-ftp-lovers, send to one of the
  509. ;;; following addresses:
  510. ;;; 
  511. ;;;     ange-ftp-lovers@anorman.hpl.hp.com
  512. ;;; or
  513. ;;;     ange-ftp-lovers%anorman.hpl.hp.com@hplb.hpl.hp.com
  514. ;;;
  515. ;;; Alternatively, there is a mailing list that only gets announcements of new
  516. ;;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
  517. ;;; subscribed to by e-mailing to the -request address as above.  Please make
  518. ;;; it clear in the request which mailing list you wish to join.
  519.  
  520. ;;; The latest version of ange-ftp can usually be obtained via anonymous ftp
  521. ;;; from:
  522. ;;;     alpha.gnu.ai.mit.edu:ange-ftp/ange-ftp.tar.Z
  523. ;;; or:
  524. ;;;     ugle.unit.no:/pub/gnu/emacs-lisp/ange-ftp.tar.Z
  525. ;;; or:
  526. ;;;   archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages/ange-ftp.tar.Z
  527.  
  528. ;;; The archives for ange-ftp-lovers can be found via anonymous ftp under:
  529. ;;;
  530. ;;;     ftp.reed.edu:pub/mailing-lists/ange-ftp/
  531.  
  532. ;;; -----------------------------------------------------------
  533. ;;; Technical information on this package:
  534. ;;; -----------------------------------------------------------
  535.  
  536. ;;; The following GNU Emacs functions are replaced by this package:
  537. ;;;
  538. ;;;   write-region
  539. ;;;   insert-file-contents
  540. ;;;   dired-readin
  541. ;;;   dired-revert
  542. ;;;   dired-call-process
  543. ;;;   diff
  544. ;;;   delete-file
  545. ;;;   read-file-name-internal
  546. ;;;   verify-visited-file-modtime
  547. ;;;   directory-files
  548. ;;;   backup-buffer
  549. ;;;   file-directory-p
  550. ;;;   file-writable-p
  551. ;;;   file-exists-p
  552. ;;;   file-readable-p
  553. ;;;   file-symlink-p
  554. ;;;   file-attributes
  555. ;;;   copy-file
  556. ;;;   rename-file
  557. ;;;   file-name-as-directory
  558. ;;;   file-name-directory
  559. ;;;   file-name-nondirectory
  560. ;;;   file-name-completion
  561. ;;;   directory-file-name
  562. ;;;   expand-file-name
  563. ;;;   file-name-all-completions
  564.  
  565. ;;; LISPDIR ENTRY for the Elisp Archive
  566. ;;; 
  567. ;;;    LCD Archive Entry:
  568. ;;;    ange-ftp|Andy Norman|ange@hplb.hpl.hp.com
  569. ;;;    |transparent FTP Support for GNU Emacs
  570. ;;;    |Date: 92/08/14 17:04:34 |Revision: 4.20 |
  571.  
  572. ;;; Checklist for adding non-UNIX support for TYPE
  573. ;;; 
  574. ;;; The following functions may need TYPE versions:
  575. ;;; (not all functions will be needed for every OS)
  576. ;;;
  577. ;;; ange-ftp-fix-path-for-TYPE
  578. ;;; ange-ftp-fix-dir-path-for-TYPE
  579. ;;; ange-ftp-TYPE-host
  580. ;;; ange-ftp-TYPE-add-host
  581. ;;; ange-ftp-parse-TYPE-listing
  582. ;;; ange-ftp-TYPE-delete-file-entry
  583. ;;; ange-ftp-TYPE-add-file-entry
  584. ;;; ange-ftp-TYPE-file-name-as-directory
  585. ;;;
  586. ;;; Variables:
  587. ;;;
  588. ;;; ange-ftp-TYPE-host-regexp
  589. ;;; May need to add TYPE to ange-ftp-dumb-host-types
  590. ;;;
  591. ;;; Check the following functions for OS dependent coding:
  592. ;;;
  593. ;;; ange-ftp-host-type
  594. ;;; ange-ftp-guess-host-type
  595. ;;; ange-ftp-allow-child-lookup
  596. ;;;
  597. ;;; For Tree Dired support:
  598. ;;;
  599. ;;; ange-ftp-dired-TYPE-insert-headerline
  600. ;;; ange-ftp-dired-TYPE-move-to-filename
  601. ;;; ange-ftp-dired-TYPE-move-to-end-of-filename
  602. ;;; ange-ftp-dired-TYPE-get-filename
  603. ;;; ange-ftp-dired-TYPE-between-files
  604. ;;; ange-ftp-TYPE-make-compressed-filename
  605. ;;; ange-ftp-dired-TYPE-ls-trim
  606. ;;; ange-ftp-TYPE-bob-version
  607. ;;; ange-ftp-dired-TYPE-clean-directory
  608. ;;; ange-ftp-dired-TYPE-flag-backup-files
  609. ;;; ange-ftp-dired-TYPE-backup-diff
  610. ;;;
  611. ;;; Variables for dired:
  612. ;;;
  613. ;;; ange-ftp-dired-TYPE-re-exe
  614. ;;; ange-ftp-dired-TYPE-re-dir
  615.  
  616. ;;; Host type conventions:
  617. ;;;
  618. ;;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
  619. ;;; (mostly) follow the following conventions for remote host types.  At
  620. ;;; least, I think that future code should try to follow these conventions,
  621. ;;; and the current code should eventually be made compliant.
  622. ;;;
  623. ;;; nil = local host type, whatever that is (probably unix).
  624. ;;;       Think nil as in "not a remote host". This value is used by
  625. ;;;       ange-ftp-dired-host-type for local buffers.
  626. ;;;
  627. ;;; t = a remote host of unknown type. Think t is in true, it's remote.
  628. ;;;     Currently, 'unix is used as the default remote host type.
  629. ;;;     Maybe we should use t.
  630. ;;;
  631. ;;; 'type = a remote host of TYPE type.
  632. ;;;
  633. ;;; 'type:list = a remote host of TYPE type, using a specialized ftp listing
  634. ;;;              program called list. This is currently only used for Unix
  635. ;;;              dl (descriptive listings), when ange-ftp-dired-host-type
  636. ;;;              is set to 'unix:dl.
  637.  
  638. ;;; Bug report codes:
  639. ;;;
  640. ;;; Because of their naive faith in this code, there are certain situations
  641. ;;; which the writers of this program believe could never happen. However,
  642. ;;; being realists they have put calls to 'error in the program at these
  643. ;;; points. These errors provide a code, which is an integer, greater than 1.
  644. ;;; To aid debugging.  the error codes, and the functions in which they reside
  645. ;;; are listed below.
  646. ;;; 
  647. ;;; 1: See ange-ftp-ls
  648. ;;;
  649.  
  650. ;;; XEmacs changes from 4.20
  651. ;;;
  652. ;;; - added gzip support
  653. ;;; - added "lazy" messages
  654. ;;; - fixed completion list in the root dir (nil vs (nil))
  655. ;;; - use (message nil) to repaint minibuf instead of that awful kludge
  656. ;;; - call compute-buffer-file-truename to set truenames properly for
  657. ;;;   when find-file-compare-truenames is set
  658. ;;; - make-directory takes a second optional argument
  659. ;;; - made ange-ftp-overwrite-fn use the 19.8 interface to byte-code objects
  660. ;;; - made ange-ftp-shell-mode work better with the latest comint
  661. ;;; - insert-file-contents takes 2-5 args in v19
  662. ;;; - moved invocation of shell-mode to get along with the latest shell-font.el
  663. ;;; - implemented ange-ftp-read-passwd in terms of read-passwd (from passwd.el)
  664. ;;; - initialize all buffer-local variables to nil
  665. ;;; - Apollo stuff from Bob Weiner
  666.  
  667.  
  668. ;;; -----------------------------------------------------------
  669. ;;; Hall of fame:
  670. ;;; -----------------------------------------------------------
  671. ;;; 
  672. ;;; Thanks to Roland McGrath for improving the filename syntax handling,
  673. ;;; for suggesting many enhancements and for numerous cleanups to the code.
  674. ;;;
  675. ;;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
  676. ;;;
  677. ;;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
  678. ;;; dired / shell auto-loading.
  679. ;;;
  680. ;;; Thanks to Sebastian Kremer for tree dired support and for many ideas and
  681. ;;; bugfixes.
  682. ;;;
  683. ;;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
  684. ;;; VOS support, and hostname completion.
  685. ;;;
  686. ;;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
  687. ;;; with file-name expansion, efficiency worries, stylistic concerns and many
  688. ;;; bugfixes.
  689. ;;;
  690. ;;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
  691. ;;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
  692. ;;; auto-recognition of the host type.
  693. ;;;
  694. ;;; Thanks to Dave Smith who wrote the info file for ange-ftp.
  695. ;;;
  696. ;;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
  697. ;;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
  698. ;;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
  699. ;;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
  700. ;;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
  701. ;;; whose names I've forgotten who have helped to debug and fix problems with
  702. ;;; ange-ftp.el.
  703.  
  704. ;;;; ------------------------------------------------------------
  705. ;;;; User customization variables.
  706. ;;;; ------------------------------------------------------------
  707.  
  708. ;;;###autoload
  709. (defvar ange-ftp-path-format
  710.   '("^/\\(\\([^@/:]*\\)@\\)?\\([^@/:]*\\):\\(.*\\)" . (3 2 4))
  711.   "*Format of a fully expanded remote pathname.  This is a cons
  712. \(REGEXP . \(HOST USER PATH\)\), where REGEXP is a regular expression matching
  713. the full remote pathname, and HOST, USER, and PATH are the numbers of
  714. parenthesized expressions in REGEXP for the components (in that order).")
  715.  
  716. ;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
  717. ;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
  718. ;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.
  719.  
  720. ;; XEmacs patch from Bob Weiner
  721. (defvar ange-ftp-multi-msgs
  722.   "^331-\\|^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^530-\\|^4[25]1-"
  723.   "*Regular expression matching messages from the ftp process that start
  724. a multiline reply.")
  725.  
  726. (defvar ange-ftp-good-msgs
  727.   "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark"
  728.   "*Regular expression matching messages from the ftp process that indicate
  729. that the action that was initiated has completed successfully.")
  730.  
  731. ;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
  732. ;; Also CMS machines use a multiline 550- reply to say that you
  733. ;; don't have write permission. ange-ftp gets into multi-line skip
  734. ;; mode and hangs. Have it ignore 550- instead. It will then barf
  735. ;; when it gets the 550 line, as it should.
  736.  
  737. (defvar ange-ftp-skip-msgs
  738.   (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
  739.       "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
  740.       "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye")
  741.   "*Regular expression matching messages from the ftp process that can be
  742. ignored.")
  743.  
  744. (defvar ange-ftp-fatal-msgs
  745.   (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
  746.       "^No control connection\\|unknown host\\|^lost connection")
  747.   "*Regular expression matching messages from the FTP process that indicate
  748. something has gone drastically wrong attempting the action that was
  749. initiated and that the FTP process should (or already has) been killed.")
  750.  
  751. (defvar ange-ftp-gateway-fatal-msgs
  752.   "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
  753.   "*Regular expression matching messages from the rlogin / telnet process that
  754. indicates that logging in to the gateway machine has gone wrong.")
  755.  
  756. (defvar ange-ftp-xfer-size-msgs
  757.   "^150 .* connection for .* (\\([0-9]+\\) bytes)"
  758.   "*Regular expression used to determine the number of bytes in a FTP transfer.")
  759.  
  760. (defvar ange-ftp-tmp-name-template "/tmp/ange-ftp"
  761.   "*Template used to create temporary files.")
  762.  
  763. (defvar ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
  764.   "*Template used to create temporary files when ftp-ing through a gateway.
  765. Files starting with this prefix need to be accessible from BOTH the local
  766. machine and the gateway machine, and need to have the SAME name on both
  767. machines, that is, /tmp is probably NOT what you want, since that is rarely
  768. cross-mounted.")
  769.  
  770. (defvar ange-ftp-netrc-filename "~/.netrc"
  771.   "*File in .netrc format to search for passwords.")
  772.  
  773. (defvar ange-ftp-disable-netrc-security-check nil
  774.   "*If non-nil avoid checking permissions on the .netrc file.")
  775.  
  776. (defvar ange-ftp-default-user nil
  777.   "*User name to use when none is specied in a pathname.
  778. If nil, then the name under which the user is logged in is used.
  779. If non-nil but not a string, the user is prompted for the name.")
  780.  
  781. (defvar ange-ftp-default-password nil
  782.   "*Password to use when the user is the same as ange-ftp-default-user.")
  783.  
  784. (defvar ange-ftp-default-account nil
  785.   "*Account password to use when the user is the same as ange-ftp-default-user.")
  786.  
  787. (defvar ange-ftp-generate-anonymous-password nil
  788.   "*If t, use a password of user@host when logging in as the anonymous user.
  789. If a string then use that as the password.
  790. If nil then prompt the user for a password.")
  791.  
  792. (defvar ange-ftp-dumb-unix-host-regexp nil
  793.   "*If non-nil, if the host being ftp'd to matches this regexp then the FTP
  794. process uses the \'dir\' command to get directory information.")
  795.  
  796. (defvar ange-ftp-binary-file-name-regexp
  797.   (concat "\\.g?z$\\|\\.Z$\\|\\.lzh$\\|\\.arc$\\|\\.zip$\\|\\.zoo$\\|\\.tar$\\|"
  798.       "\\.dvi$\\|\\.ps$\\|\\.elc$\\|TAGS$\\|\\.gif$\\|"
  799.       "\\.EXE\\(;[0-9]+\\)?$\\|\\.g?z-part-..$\\|\\.Z-part-..$")
  800.   "*If a file matches this regexp then it is transferred in binary mode.")
  801.  
  802. (defvar ange-ftp-gateway-host nil
  803.   "*Name of host to use as gateway machine when local FTP isn't possible.")
  804.  
  805. (defvar ange-ftp-local-host-regexp ".*"
  806.   "*If a host being FTP'd to matches this regexp then the ftp process is started
  807. locally, otherwise the FTP process is started on \`ange-ftp-gateway-host\'
  808. instead.")
  809.  
  810. (defvar ange-ftp-gateway-program-interactive nil
  811.   "*If non-nil then the gateway program is expected to connect to the gateway
  812. machine and eventually give a shell prompt.  Both telnet and rlogin do something
  813. like this.")
  814.  
  815. (defvar ange-ftp-gateway-program (if (eq system-type 'hpux) "remsh" "rsh")
  816.   "*Name of program to spawn a shell on the gateway machine.  Valid candidates
  817. are rsh (remsh on hp-ux), telnet and rlogin.  See also the gateway variable
  818. above.")
  819.  
  820. (defvar ange-ftp-gateway-prompt-pattern "^[^#$%>;]*[#$%>;] *"
  821.   "*Regexp used to detect that the logging-in sequence is completed on the
  822. gateway machine and that the shell is now awaiting input.  Make this regexp as
  823. strict as possible; it shouldn't match *anything* at all except the user's
  824. initial prompt.  The above string will fail under most SUN-3's since it
  825. matches the login banner.")
  826.  
  827. (defvar ange-ftp-gateway-setup-term-command
  828.   (if (eq system-type 'hpux)
  829.       "stty -onlcr -echo\n"
  830.     "stty -echo nl\n")
  831.   "*Command to use after logging in to the gateway machine to stop the terminal
  832. echoing each command and to strip out trailing ^M characters.")
  833.  
  834. (defvar ange-ftp-smart-gateway nil
  835.   "*If the gateway FTP is smart enough to use proxy server, then don't bother
  836. telnetting etc, just issue a user@host command instead.")
  837.  
  838. (defvar ange-ftp-smart-gateway-port "21"
  839.   "*Port on gateway machine to use when smart gateway is in operation.")
  840.  
  841. (defvar ange-ftp-send-hash t
  842.   "*If non-nil, send the HASH command to the FTP client.")
  843.  
  844. (defvar ange-ftp-binary-hash-mark-size nil
  845.   "*Default size, in bytes, between hash-marks when transferring a binary file.
  846. If NIL, this variable will be locally overridden if the FTP client outputs a
  847. suitable response to the HASH command.  If non-NIL then this value takes
  848. precedence over the local value.")
  849.  
  850. (defvar ange-ftp-ascii-hash-mark-size 1024
  851.   "*Default size, in bytes, between hash-marks when transferring an ASCII file.
  852. This variable is buffer-local and will be locally overridden if the FTP client
  853. outputs a suitable response to the HASH command.")
  854.  
  855. (defvar ange-ftp-process-verbose t
  856.   "*If non-NIL then be chatty about interaction with the FTP process.")
  857.  
  858. (defvar ange-ftp-ftp-program-name "ftp"
  859.   "*Name of FTP program to run.")
  860.  
  861. (defvar ange-ftp-gateway-ftp-program-name "ftp"
  862.   "*Name of FTP program to run on gateway machine.
  863. Some AT&T folks claim to use something called `pftp' here.")
  864.  
  865. (defvar ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
  866.   "*A list of arguments passed to the FTP program when started.")
  867.  
  868. (defvar ange-ftp-nslookup-program nil
  869.   "*If non-NIL then a string naming nslookup program." )
  870.  
  871. (defvar ange-ftp-make-backup-files ()
  872.   "*A list of operating systems for which ange-ftp will make Emacs backup
  873. files files on the remote host. For example, '\(unix\) makes sense, but
  874. '\(unix vms\) or '\(vms\) would be silly, since vms makes its own backups.")
  875.  
  876. (defvar ange-ftp-retry-time 5
  877.   "*Number of seconds to wait before retrying if a file or listing
  878. doesn't arrive. This might need to be increased for very slow connections.")
  879.  
  880. (defvar ange-ftp-auto-save 0
  881.   "If 1, allows ange-ftp files to be auto-saved.
  882. If 0, suppresses auto-saving of ange-ftp files.
  883. Don't use any other value.")
  884.  
  885. ;;;; ------------------------------------------------------------
  886. ;;;; Hash table support.
  887. ;;;; ------------------------------------------------------------
  888.  
  889. (require 'backquote)
  890.  
  891. (defun ange-ftp-make-hashtable (&optional size)
  892.   "Make an obarray suitable for use as a hashtable.
  893. SIZE, if supplied, should be a prime number."
  894.   (make-vector (or size 31) 0))
  895.  
  896. (defun ange-ftp-map-hashtable (fun tbl)
  897.   "Call FUNCTION on each key and value in HASHTABLE."
  898.   (mapatoms
  899.    (function 
  900.     (lambda (sym)
  901.       (funcall fun (get sym 'key) (get sym 'val))))
  902.    tbl))
  903.  
  904. (defmacro ange-ftp-make-hash-key (key)
  905.   "Convert KEY into a suitable key for a hashtable."
  906.   (` (if (stringp (, key))
  907.      (, key)
  908.        (prin1-to-string (, key)))))
  909.  
  910. (defun ange-ftp-get-hash-entry (key tbl)
  911.   "Return the value associated with KEY in HASHTABLE."
  912.   (let ((sym (intern-soft (ange-ftp-make-hash-key key) tbl)))
  913.     (and sym (get sym 'val))))
  914.  
  915. (defun ange-ftp-put-hash-entry (key val tbl)
  916.   "Record an association between KEY and VALUE in HASHTABLE."
  917.   (let ((sym (intern (ange-ftp-make-hash-key key) tbl)))
  918.     (put sym 'val val)
  919.     (put sym 'key key)))
  920.  
  921. (defun ange-ftp-del-hash-entry (key tbl)
  922.   "Copy all symbols except KEY in HASHTABLE and return modified hashtable."
  923.   (let* ((len (length tbl))
  924.      (new-tbl (ange-ftp-make-hashtable len))
  925.      (i (1- len)))
  926.     (ange-ftp-map-hashtable
  927.      (function
  928.       (lambda (k v)
  929.     (or (equal k key)
  930.         (ange-ftp-put-hash-entry k v new-tbl))))
  931.      tbl)
  932.     (while (>= i 0)
  933.       (aset tbl i (aref new-tbl i))
  934.       (setq i (1- i)))
  935.     tbl))
  936.  
  937. (defun ange-ftp-hash-entry-exists-p (key tbl)
  938.   "Return whether there is an association for KEY in TABLE."
  939.   (intern-soft (ange-ftp-make-hash-key key) tbl))
  940.  
  941. (defun ange-ftp-hash-table-keys (tbl)
  942.   "Return a sorted list of all the active keys in the hashtable, as strings."
  943.   (sort (all-completions "" tbl)
  944.     (function string-lessp)))
  945.  
  946. ;;;; ------------------------------------------------------------
  947. ;;;; Internal variables.
  948. ;;;; ------------------------------------------------------------
  949.  
  950. (defconst ange-ftp-version "Revision: 4.20.XEmacs")
  951.  
  952. (defvar ange-ftp-data-buffer-name " *ftp data*"
  953.   "Buffer name to hold directory listing data received from ftp process.")
  954.  
  955. (defvar ange-ftp-netrc-modtime nil
  956.   "Last modified time of the netrc file from file-attributes.")
  957.  
  958. (defvar ange-ftp-user-hashtable (ange-ftp-make-hashtable)
  959.   "Hash table holding associations between HOST, USER pairs.")
  960.  
  961. (defvar ange-ftp-passwd-hashtable (ange-ftp-make-hashtable)
  962.   "Mapping between a HOST, USER pair and a PASSWORD for them.")
  963.  
  964. (defvar ange-ftp-account-hashtable (ange-ftp-make-hashtable)
  965.   "Mapping between a HOST, USER pair and a ACCOUNT password for them.")
  966.  
  967. (defvar ange-ftp-files-hashtable (ange-ftp-make-hashtable 97)
  968.   "Hash table for storing directories and their respective files.")
  969.  
  970. (defvar ange-ftp-ls-cache-lsargs nil
  971.   "Last set of args used by ange-ftp-ls.")
  972.  
  973. (defvar ange-ftp-ls-cache-file nil
  974.   "Last file passed to ange-ftp-ls.")
  975.  
  976. (defvar ange-ftp-ls-cache-res nil
  977.   "Last result returned from ange-ftp-ls.")
  978.  
  979. ;; New error symbols.
  980. (put 'ftp-error 'error-conditions '(ftp-error file-error error))
  981. ;; (put 'ftp-error 'error-message "FTP error")
  982.  
  983. ;;; ------------------------------------------------------------
  984. ;;; Match-data support (stolen from Kyle I think)
  985. ;;; ------------------------------------------------------------
  986.  
  987. (defmacro ange-ftp-save-match-data (&rest body)
  988.   "Execute the BODY forms, restoring the global value of the match data.
  989. Before executing BODY, case-fold-search is locally bound to nil."
  990.   (let ((original (make-symbol "match-data"))
  991.     case-fold-search)
  992.     (list
  993.      'let (list (list original '(match-data)))
  994.      (list 'unwind-protect
  995.            (cons 'progn body)
  996.            (list 'store-match-data original)))))
  997.  
  998. (put 'ange-ftp-save-match-data 'lisp-indent-hook 0)
  999. (put 'ange-ftp-save-match-data 'edebug-form-hook '(&rest form))
  1000.  
  1001. ;;; ------------------------------------------------------------
  1002. ;;; Enhanced message support.
  1003. ;;; ------------------------------------------------------------
  1004.  
  1005. (defun ange-ftp-message (fmt &rest args)
  1006.   "Output the given message, but truncate to the size of the minibuffer
  1007. window."
  1008.   (let ((msg (apply (function format) fmt args))
  1009.     (max (window-width (minibuffer-window))))
  1010.     (if (>= (length msg) max)
  1011.     (setq msg (concat "> " (substring msg (- 3 max)))))
  1012.     (message "%s" msg)))
  1013.  
  1014. (defvar ange-ftp-lazy-message-time 0)
  1015. (defun ange-ftp-lazy-message (fmt &rest args)
  1016.   "Output the given message, but truncate to the size of the minibuffer
  1017. window, and don't print the message if we've printed another message
  1018. less than one second ago."
  1019.   (if (= ange-ftp-lazy-message-time
  1020.      (setq ange-ftp-lazy-message-time (nth 1 (current-time))))
  1021.       nil
  1022.     (apply 'ange-ftp-message fmt args)))
  1023.  
  1024. (or (fboundp 'current-time) (fset 'ange-ftp-lazy-message 'ange-ftp-message))
  1025.  
  1026.  
  1027. (defun ange-ftp-abbreviate-filename (file &optional new)
  1028.   "Abbreviate the given filename relative to the default-directory.  If the
  1029. optional parameter NEW is given and the non-directory parts match, only return
  1030. the directory part of the file."
  1031.   (ange-ftp-save-match-data
  1032.     (if (and default-directory
  1033.          (string-match (concat "^"
  1034.                    (regexp-quote default-directory)
  1035.                    ".") file))
  1036.     (setq file (substring file (1- (match-end 0)))))
  1037.     (if (and new
  1038.          (string-equal (file-name-nondirectory file)
  1039.                (file-name-nondirectory new)))
  1040.     (setq file (file-name-directory file)))
  1041.     (or file "./")))
  1042.  
  1043. ;;;; ------------------------------------------------------------
  1044. ;;;; User / Host mapping support.
  1045. ;;;; ------------------------------------------------------------
  1046.  
  1047. (defun ange-ftp-set-user (host user)
  1048.   "For a given HOST, set or change the default USER."
  1049.   (interactive "sHost: \nsUser: ")
  1050.   (ange-ftp-put-hash-entry host user ange-ftp-user-hashtable))
  1051.  
  1052. (defun ange-ftp-get-user (host)
  1053.   "Given a HOST, return the default USER."
  1054.   (ange-ftp-parse-netrc)
  1055.   (let ((user (ange-ftp-get-hash-entry host ange-ftp-user-hashtable)))
  1056.     (or user
  1057.     (prog1
  1058.         (setq user
  1059.           (cond ((stringp ange-ftp-default-user)
  1060.              ;; We have a default name.  Use it.
  1061.              ange-ftp-default-user)
  1062.             (ange-ftp-default-user
  1063.              ;; Ask the user.
  1064.              (let ((enable-recursive-minibuffers t))
  1065.                (read-string (format "User for %s: " host)
  1066.                     (user-login-name))))
  1067.             ;; Default to the user's login name.
  1068.             (t
  1069.              (user-login-name))))
  1070.       (ange-ftp-set-user host user)))))
  1071.  
  1072. ;;;; ------------------------------------------------------------
  1073. ;;;; Password support.
  1074. ;;;; ------------------------------------------------------------
  1075.  
  1076. (defun ange-ftp-read-passwd (prompt &optional default)
  1077.   "Read a password from the user.
  1078. See documentation of `read-passwd' for more info."
  1079.   (read-passwd prompt nil default))
  1080.  
  1081. ;(defun ange-ftp-read-passwd (prompt &optional default)
  1082. ;  "Read a password from the user. Echos a . for each character typed.
  1083. ;End with RET, LFD, or ESC. DEL or C-h rubs out.  ^U kills line.
  1084. ;Optional DEFAULT is password to start with."
  1085. ;  (let ((pass (if default default ""))
  1086. ;    (c 0)
  1087. ;    (echo-keystrokes 0)
  1088. ;    (cursor-in-echo-area t))
  1089. ;    (while (and (/= c ?\r) (/= c ?\n) (/= c ?\e))
  1090. ;      (message "%s%s"
  1091. ;           prompt
  1092. ;           (make-string (length pass) ?.))
  1093. ;      (setq c (read-char))
  1094. ;      (if (= c ?\C-u)
  1095. ;      (setq pass "")
  1096. ;    (if (and (/= c ?\b) (/= c ?\177))
  1097. ;        (setq pass (concat pass (char-to-string c)))
  1098. ;      (if (> (length pass) 0)
  1099. ;          (setq pass (substring pass 0 -1))))))
  1100. ;    (ange-ftp-repaint-minibuffer)
  1101. ;    (substring pass 0 -1)))
  1102.  
  1103. (defmacro ange-ftp-generate-passwd-key (host user)
  1104.   (` (concat (, host) "/" (, user))))
  1105.  
  1106. (defmacro ange-ftp-lookup-passwd (host user)
  1107.   (` (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key (, host) (, user))
  1108.                   ange-ftp-passwd-hashtable)))
  1109.  
  1110. (defun ange-ftp-set-passwd (host user passwd)
  1111.   "For a given HOST and USER, set or change the associated PASSWORD."
  1112.   (interactive (list (read-string "Host: ")
  1113.              (read-string "User: ")
  1114.              (ange-ftp-read-passwd "Password: ")))
  1115.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1116.                passwd
  1117.                ange-ftp-passwd-hashtable))
  1118.  
  1119. (defun ange-ftp-get-host-with-passwd (user)
  1120.   "Given a USER, return a host we know the password for."
  1121.   (ange-ftp-parse-netrc)
  1122.   (catch 'found-one
  1123.     (ange-ftp-map-hashtable
  1124.      (function (lambda (host val)
  1125.          (if (ange-ftp-lookup-passwd host user)
  1126.              (throw 'found-one host))))
  1127.      ange-ftp-user-hashtable)
  1128.     (ange-ftp-save-match-data
  1129.       (ange-ftp-map-hashtable
  1130.        (function
  1131.     (lambda (key value)
  1132.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1133.           (let ((host (substring key 0 (match-beginning 1))))
  1134.         (if (and (string-equal user (substring key (match-end 1)))
  1135.              value)
  1136.             (throw 'found-one host))))))
  1137.        ange-ftp-passwd-hashtable))
  1138.     nil))
  1139.  
  1140. (defun ange-ftp-get-passwd (host user)
  1141.   "Given a HOST and USER, return the FTP password, prompting if it was not
  1142. previously set."
  1143.   (ange-ftp-parse-netrc)
  1144.  
  1145.   ;; look up password in the hash table first; user might have overriden the
  1146.   ;; defaults.
  1147.   (cond ((ange-ftp-lookup-passwd host user))
  1148.     
  1149.     ;; see if default user and password set from the .netrc file.
  1150.     ((and (stringp ange-ftp-default-user)
  1151.           ange-ftp-default-password
  1152.           (string-equal user ange-ftp-default-user))
  1153.      ange-ftp-default-password)
  1154.     
  1155.     ;; anonymous ftp password is handled specially since there is an
  1156.     ;; unwritten rule about how that is used on the Internet.
  1157.     ((and (or (string-equal user "anonymous")
  1158.           (string-equal user "ftp"))
  1159.           ange-ftp-generate-anonymous-password)
  1160.      (if (stringp ange-ftp-generate-anonymous-password)
  1161.          ange-ftp-generate-anonymous-password
  1162.        (concat (user-login-name) "@" (system-name))))
  1163.     
  1164.     ;; see if same user has logged in to other hosts; if so then prompt
  1165.     ;; with the password that was used there.
  1166.     (t
  1167.      (let* ((other (ange-ftp-get-host-with-passwd user))
  1168.         (passwd (if other
  1169.                 
  1170.                 ;; found another machine with the same user.
  1171.                 ;; Try that account.
  1172.                 (ange-ftp-read-passwd
  1173.                  (format "passwd for %s@%s (same as %s@%s): "
  1174.                      user host user other)
  1175.                  (ange-ftp-lookup-passwd other user))
  1176.               
  1177.               ;; I give up.  Ask the user for the password.
  1178.               (ange-ftp-read-passwd
  1179.                (format "Password for %s@%s: " user host)))))
  1180.        (ange-ftp-set-passwd host user passwd)
  1181.        passwd))))
  1182.  
  1183. ;;;; ------------------------------------------------------------
  1184. ;;;; Account support
  1185. ;;;; ------------------------------------------------------------
  1186.  
  1187. ;; Account passwords must be either specified in the .netrc file, or set
  1188. ;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
  1189. ;; check to see whether the FTP process is actually prompting for an account
  1190. ;; password.
  1191.  
  1192. (defun ange-ftp-set-account (host user account)
  1193.   "For a given HOST and USER, set or change the associated ACCOUNT password."
  1194.   (interactive (list (read-string "Host: ")
  1195.              (read-string "User: ")
  1196.              (ange-ftp-read-passwd "Account password: ")))
  1197.   (ange-ftp-put-hash-entry (ange-ftp-generate-passwd-key host user)
  1198.                account
  1199.                ange-ftp-account-hashtable))
  1200.  
  1201. (defun ange-ftp-get-account (host user)
  1202.   "Given a HOST and USER, return the FTP account."
  1203.   (ange-ftp-parse-netrc)
  1204.   (or (ange-ftp-get-hash-entry (ange-ftp-generate-passwd-key host user)
  1205.                    ange-ftp-account-hashtable)
  1206.       (and (stringp ange-ftp-default-user)
  1207.        (string-equal user ange-ftp-default-user)
  1208.        ange-ftp-default-account)))
  1209.  
  1210. ;;;; ------------------------------------------------------------
  1211. ;;;; ~/.netrc support
  1212. ;;;; ------------------------------------------------------------
  1213.  
  1214. (defun ange-ftp-chase-symlinks (file)
  1215.   "Return the filename that FILENAME references, following all symbolic links."
  1216.   (let (temp)
  1217.     (while (setq temp (ange-ftp-real-file-symlink-p file))
  1218.       (setq file
  1219.         (if (file-name-absolute-p temp)
  1220.         temp
  1221.           (concat (file-name-directory file) temp)))))
  1222.   file)
  1223.  
  1224. (defun ange-ftp-parse-netrc-token (token limit)
  1225.   "Move along current line looking for the value of the TOKEN.  Valid
  1226. separators between TOKEN and its value are commas and whitespace.
  1227. Second arg LIMIT is a limit for the search."
  1228.   (if (search-forward token limit t)
  1229.       (let (beg)
  1230.     (skip-chars-forward ", \t\r\n" limit)
  1231.     (if (eq (following-char) ?\")    ;quoted token value
  1232.         (progn (forward-char 1)
  1233.            (setq beg (point))
  1234.            (skip-chars-forward "^\"" limit)
  1235.            (forward-char 1)
  1236.            (buffer-substring beg (1- (point))))
  1237.       (setq beg (point))
  1238.       (skip-chars-forward "^, \t\r\n" limit)
  1239.       (buffer-substring beg (point))))))
  1240.  
  1241. (defun ange-ftp-parse-netrc-group ()
  1242.   "Extract the values for the tokens  \`machine\', \`login\', \`password\'
  1243. and \`account\' in the current buffer.  If successful, record the information
  1244. found."
  1245.   (beginning-of-line)
  1246.   (let ((start (point))
  1247.     (end (progn (re-search-forward "machine\\|default"
  1248.                        (point-max) 'end 2) (point)))
  1249.     machine login password account)
  1250.     (goto-char start)
  1251.     (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
  1252.       login    (ange-ftp-parse-netrc-token "login"    end)
  1253.       password (ange-ftp-parse-netrc-token "password" end)
  1254.       account  (ange-ftp-parse-netrc-token "account"  end))
  1255.     (if (and machine login)
  1256.     ;; found a `machine` token.
  1257.     (progn
  1258.       (ange-ftp-set-user machine login)
  1259.       (ange-ftp-set-passwd machine login password)
  1260.       (and account
  1261.            (ange-ftp-set-account machine login account)))
  1262.       (goto-char start)
  1263.       (if (search-forward "default" end t)
  1264.       ;; found a `default' token
  1265.       (progn
  1266.         (setq login    (ange-ftp-parse-netrc-token "login"    end)
  1267.           password (ange-ftp-parse-netrc-token "password" end)
  1268.           account  (ange-ftp-parse-netrc-token "account"  end))
  1269.         (and login
  1270.          (setq ange-ftp-default-user login))
  1271.         (and password
  1272.          (setq ange-ftp-default-password password))
  1273.         (and account
  1274.          (setq ange-ftp-default-account account)))))
  1275.     (goto-char end)))
  1276.  
  1277. (defun ange-ftp-parse-netrc ()
  1278.   "If ~/.netrc file exists and has the correct permissions then extract the
  1279. \`machine\', \`login\', \`password\' and \`account\' information from within."
  1280.  
  1281.   ;; We set this before actually doing it to avoid the possibility
  1282.   ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  1283.   (interactive)
  1284.   (let* ((file (ange-ftp-chase-symlinks
  1285.         (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
  1286.      (attr (ange-ftp-real-file-attributes file)))
  1287.     (if (and attr            ; file exists.
  1288.          (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))    ; file changed
  1289.     (ange-ftp-save-match-data
  1290.       (if (or ange-ftp-disable-netrc-security-check
  1291.           (and (eq (nth 2 attr) (user-uid)) ; Same uids.
  1292.                (string-match ".r..------" (nth 8 attr))))
  1293.           (save-excursion
  1294.         ;; we are cheating a bit here.  I'm trying to do the equivalent
  1295.         ;; of find-file on the .netrc file, but then nuke it afterwards.
  1296.         ;; with the bit of logic below we should be able to have
  1297.         ;; encrypted .netrc files.
  1298.         (set-buffer (generate-new-buffer "*ftp-.netrc*"))
  1299.         (ange-ftp-real-insert-file-contents file)
  1300.         (setq buffer-file-name file)
  1301.         (setq default-directory (file-name-directory file))
  1302.         (normal-mode t)
  1303.         (mapcar 'funcall find-file-hooks)
  1304.         (setq buffer-file-name nil)
  1305.         (goto-char (point-min))
  1306.         (while (not (eobp))
  1307.           (ange-ftp-parse-netrc-group))
  1308.         (kill-buffer (current-buffer)))
  1309.         (ange-ftp-message "%s either not owned by you or badly protected."
  1310.                   ange-ftp-netrc-filename)
  1311.         (sit-for 1))
  1312.       (setq ange-ftp-netrc-modtime (nth 5 attr))))))
  1313.  
  1314. (defun ange-ftp-generate-root-prefixes ()
  1315.   "Return a list of prefixes of the form 'user@host:' to be used when
  1316. completion is done in the root directory."
  1317.   (ange-ftp-parse-netrc)
  1318.   (ange-ftp-save-match-data
  1319.     (let (res)
  1320.       (ange-ftp-map-hashtable
  1321.        (function
  1322.     (lambda (key value)
  1323.       (if (string-match "^[^/]*\\(/\\).*$" key)
  1324.           (let ((host (substring key 0 (match-beginning 1)))
  1325.             (user (substring key (match-end 1))))
  1326.         (setq res (cons (list (concat user "@" host ":"))
  1327.                 res))))))
  1328.        ange-ftp-passwd-hashtable)
  1329.       (ange-ftp-map-hashtable
  1330.        (function (lambda (host user)
  1331.            (setq res (cons (list (concat host ":"))
  1332.                    res))))
  1333.        ange-ftp-user-hashtable)
  1334. ;;      (or res (list nil))
  1335.       res
  1336.       )))
  1337.  
  1338. ;;;; ------------------------------------------------------------
  1339. ;;;; Remote pathname syntax support.
  1340. ;;;; ------------------------------------------------------------
  1341.  
  1342. (defmacro ange-ftp-ftp-path-component (n ns path)
  1343.   "Extract the Nth ftp path component from NS."
  1344.   (` (let ((elt (nth (, n) (, ns))))
  1345.        (substring (, path) (match-beginning elt) (match-end elt)))))
  1346.  
  1347. (defvar ange-ftp-ftp-path-arg "")
  1348. (defvar ange-ftp-ftp-path-res nil)
  1349.  
  1350. (defun ange-ftp-ftp-path (path)
  1351.   "Parse PATH according to ange-ftp-path-format (which see).
  1352. Returns a list (HOST USER PATH), or nil if PATH does not match the format."
  1353.   (if (string-equal path ange-ftp-ftp-path-arg)
  1354.       ange-ftp-ftp-path-res
  1355.     (setq ange-ftp-ftp-path-arg path
  1356.       ange-ftp-ftp-path-res
  1357.       (ange-ftp-save-match-data
  1358.         (if (string-match (car ange-ftp-path-format) path)
  1359.         (let* ((ns (cdr ange-ftp-path-format))
  1360.                (host (ange-ftp-ftp-path-component 0 ns path))
  1361.                (user (ange-ftp-ftp-path-component 1 ns path))
  1362.                (path (ange-ftp-ftp-path-component 2 ns path)))
  1363.           (if (zerop (length user))
  1364.               (setq user (ange-ftp-get-user host)))
  1365.           (list host user path))
  1366.           nil)))))
  1367.  
  1368. (defun ange-ftp-replace-path-component (fullpath path)
  1369.   "Take a FULLPATH that matches according to ange-ftp-path-format and
  1370. replace the path component with PATH."
  1371.   (ange-ftp-save-match-data
  1372.     (if (string-match (car ange-ftp-path-format) fullpath)
  1373.     (let* ((ns (cdr ange-ftp-path-format))
  1374.            (elt (nth 2 ns)))
  1375.       (concat (substring fullpath 0 (match-beginning elt))
  1376.           path
  1377.           (substring fullpath (match-end elt)))))))
  1378.  
  1379. ;;;; ------------------------------------------------------------
  1380. ;;;; Miscellaneous utils.
  1381. ;;;; ------------------------------------------------------------
  1382.  
  1383. (setq ange-ftp-tmp-keymap (make-sparse-keymap))
  1384. (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)
  1385.  
  1386. (defun ange-ftp-repaint-minibuffer ()
  1387.   "Gross hack to set minibuf_message = 0, so that the contents of the
  1388. minibuffer will show."
  1389.   (if (eq (selected-window) (minibuffer-window))
  1390.       (if (string-match "XEmacs" emacs-version)
  1391.       (message nil)
  1392.     ;; v18 GNU Emacs
  1393.     (let ((unread-command-char ?\C-m)
  1394.           (enable-recursive-minibuffers t))
  1395.       (read-from-minibuffer "" nil ange-ftp-tmp-keymap nil)))))
  1396.  
  1397. (defun ange-ftp-ftp-process-buffer (host user)
  1398.   "Return the name of the buffer that collects output from the ftp process
  1399. connected to the given HOST and USER pair."
  1400.   (concat "*ftp " user "@" host "*"))
  1401.  
  1402. (defun ange-ftp-error (host user msg)
  1403.   "Display the last chunk of output from the ftp process for the given HOST
  1404. USER pair, and signal an error including MSG in the text."
  1405.   (let ((cur (selected-window))
  1406.     (pop-up-windows t))
  1407.     (pop-to-buffer
  1408.      (get-buffer-create
  1409.       (ange-ftp-ftp-process-buffer host user)))
  1410.     (goto-char (point-max))
  1411.     (select-window cur))
  1412.   (signal 'ftp-error (list (format "FTP Error: %s" msg))))
  1413.  
  1414. (defun ange-ftp-set-buffer-mode ()
  1415.   "Set the correct modes for the current buffer if it is visiting a remote
  1416. file."
  1417.   (if (and (stringp buffer-file-name)
  1418.        (ange-ftp-ftp-path buffer-file-name))
  1419.       (progn
  1420.     (auto-save-mode ange-ftp-auto-save)
  1421.     (make-variable-buffer-local 'revert-buffer-function)
  1422.     (setq revert-buffer-function 'ange-ftp-revert-buffer))))
  1423.  
  1424. (defun ange-ftp-kill-ftp-process (buffer)
  1425.   "If the BUFFER's visited filename or default-directory is an ftp filename
  1426. then kill the related ftp process."
  1427.   (interactive "bKill FTP process associated with buffer: ")
  1428.   (if (null buffer)
  1429.       (setq buffer (current-buffer)))
  1430.   (let ((file (or (buffer-file-name) default-directory)))
  1431.     (if file
  1432.     (let ((parsed (ange-ftp-ftp-path (expand-file-name file))))
  1433.       (if parsed
  1434.           (let ((host (nth 0 parsed))
  1435.             (user (nth 1 parsed)))
  1436.         (kill-buffer (ange-ftp-ftp-process-buffer host user))))))))
  1437.  
  1438. (defun ange-ftp-quote-string (string)
  1439.   "Quote any characters in STRING that may confuse the ftp process."
  1440.   (apply (function concat)
  1441.      (mapcar (function
  1442.            (lambda (char)
  1443.              (if (or (<= char ? )
  1444.                  (> char ?\~)
  1445.                      (= char ?\")
  1446.                  (= char ?\\))
  1447.              (vector ?\\ char)
  1448.                (vector char))))
  1449.          string)))
  1450.  
  1451. (defun ange-ftp-barf-if-not-directory (directory)
  1452.   (or (file-directory-p directory)
  1453.       (signal 'file-error
  1454.           (list "Opening directory"
  1455.             (if (file-exists-p directory)
  1456.             "not a directory"
  1457.               "no such file or directory")
  1458.             directory))))
  1459.  
  1460. ;;;; ------------------------------------------------------------
  1461. ;;;; FTP process filter support.
  1462. ;;;; ------------------------------------------------------------
  1463.  
  1464. (defun ange-ftp-process-handle-line (line proc)
  1465.   "Look at the given LINE from the ftp process PROC.  Try to catagorize it
  1466. into one of four categories: good, skip, fatal, or unknown."
  1467.   (cond ((string-match ange-ftp-xfer-size-msgs line)
  1468.      (setq ange-ftp-xfer-size
  1469.            (ash (string-to-int (substring line
  1470.                           (match-beginning 1)
  1471.                           (match-end 1)))
  1472.             -10)))
  1473.     ((string-match ange-ftp-skip-msgs line)
  1474.       (setq ange-ftp-process-multi-skip nil) ;; XEmacs patch (Bob Weiner)
  1475.      t)
  1476.     ((string-match ange-ftp-good-msgs line)
  1477.      (setq ange-ftp-process-busy nil
  1478.            ange-ftp-process-result t
  1479.            ange-ftp-process-result-line line))
  1480.     ((string-match ange-ftp-fatal-msgs line)
  1481.      (delete-process proc)
  1482.      (setq ange-ftp-process-busy nil
  1483.            ange-ftp-process-result-line line))
  1484.     ((string-match ange-ftp-multi-msgs line)
  1485.      (setq ange-ftp-process-multi-skip t))
  1486.     (ange-ftp-process-multi-skip
  1487.      t)
  1488.     (t
  1489.      (setq ange-ftp-process-busy nil
  1490.            ange-ftp-process-result-line line))))
  1491.  
  1492. (defun ange-ftp-process-log-string (proc str)
  1493.   "For a given PROCESS, log the given STRING at the end of its
  1494. associated buffer."
  1495.   (let ((old-buffer (current-buffer)))
  1496.     (unwind-protect
  1497.     (let (moving)
  1498.       (set-buffer (process-buffer proc))
  1499.       (setq moving (= (point) (process-mark proc)))
  1500.       (save-excursion
  1501.         ;; Insert the text, moving the process-marker.
  1502.         (goto-char (process-mark proc))
  1503.         (insert str)
  1504.         (set-marker (process-mark proc) (point)))
  1505.       (if moving (goto-char (process-mark proc))))
  1506.       (set-buffer old-buffer))))
  1507.  
  1508. (defun ange-ftp-set-xfer-size (host user bytes)
  1509.   "Set the size of the next FTP transfer in bytes."
  1510.   (let ((proc (ange-ftp-get-process host user)))
  1511.     (if proc
  1512.     (let ((buf (process-buffer proc)))
  1513.       (if buf
  1514.           (save-excursion
  1515.         (set-buffer buf)
  1516.         (setq ange-ftp-xfer-size (ash bytes -10))))))))
  1517.  
  1518. (defun ange-ftp-process-handle-hash (str)
  1519.   "Remove hash marks from STRING and display count so far."
  1520.   (setq str (concat (substring str 0 (match-beginning 0))
  1521.             (substring str (match-end 0)))
  1522.     ange-ftp-hash-mark-count (+ (- (match-end 0)
  1523.                        (match-beginning 0))
  1524.                     ange-ftp-hash-mark-count))
  1525.   (and ange-ftp-process-msg
  1526.        ange-ftp-process-verbose
  1527.        (not (eq (selected-window) (minibuffer-window)))
  1528.        (not (boundp 'search-message))    ;screws up isearch otherwise
  1529.        (not cursor-in-echo-area)    ;screws up y-or-n-p otherwise
  1530.        (let ((kbytes (ash (* ange-ftp-hash-mark-unit
  1531.                  ange-ftp-hash-mark-count)
  1532.               -6)))
  1533.        (if (zerop ange-ftp-xfer-size)
  1534.        (ange-ftp-lazy-message "%s...%dk" ange-ftp-process-msg kbytes)
  1535.      (let ((percent (/ (* 100 kbytes) ange-ftp-xfer-size)))
  1536.        ;; cut out the redisplay of identical %-age messages.
  1537.        (if (not (eq percent ange-ftp-last-percent))
  1538.            (progn
  1539.          (setq ange-ftp-last-percent percent)
  1540.          (ange-ftp-lazy-message "%s...%d%%"
  1541.                     ange-ftp-process-msg percent)))))))
  1542.   str)
  1543.  
  1544. (defun ange-ftp-call-cont (cont result line)
  1545.   "Call the function specified by CONT.  CONT can be either a function or a
  1546. list of a function and some args.  The first two parameters passed to the
  1547. function will be RESULT and LINE.  The remaining args will be taken from CONT
  1548. if a list was passed."
  1549.   (if cont
  1550.       (if (and (listp cont)
  1551.            (not (eq (car cont) 'lambda)))
  1552.       (apply (car cont) result line (cdr cont))
  1553.     (funcall cont result line))))
  1554.  
  1555. (defun ange-ftp-process-filter (proc str)
  1556.   "Build up a complete line of output from the ftp PROCESS and pass it
  1557. on to ange-ftp-process-handle-line to deal with."
  1558.   (let ((buffer (process-buffer proc))
  1559.     (old-buffer (current-buffer)))
  1560.  
  1561.     ;; see if the buffer is still around... it could have been deleted.
  1562.     (if (buffer-name buffer)
  1563.     (unwind-protect
  1564.         (ange-ftp-save-match-data
  1565.           (set-buffer (process-buffer proc))
  1566.           
  1567.           ;; handle hash mark printing
  1568.           (and ange-ftp-hash-mark-unit
  1569.            ange-ftp-process-busy
  1570.            (string-match "^#+$" str)
  1571.            (setq str (ange-ftp-process-handle-hash str)))
  1572.           (ange-ftp-process-log-string proc str)
  1573.           (if ange-ftp-process-busy
  1574.           (progn
  1575.             (setq ange-ftp-process-string (concat ange-ftp-process-string
  1576.                               str))
  1577.             
  1578.             ;; if we gave an empty password to the USER command earlier
  1579.             ;; then we should send a null password now.
  1580.             (if (string-match "Password: *$" ange-ftp-process-string)
  1581.             (send-string proc "\n"))))
  1582.           (while (and ange-ftp-process-busy
  1583.               (string-match "\n" ange-ftp-process-string))
  1584.         (let ((line (substring ange-ftp-process-string
  1585.                        0
  1586.                        (match-beginning 0))))
  1587.           (setq ange-ftp-process-string (substring ange-ftp-process-string
  1588.                                (match-end 0)))
  1589.           (while (string-match "^ftp> *" line)
  1590.             (setq line (substring line (match-end 0))))
  1591.           (ange-ftp-process-handle-line line proc)))
  1592.  
  1593.           ;; has the ftp client finished?  if so then do some clean-up
  1594.           ;; actions.
  1595.           (if (not ange-ftp-process-busy)
  1596.           (progn
  1597.             ;; reset the xfer size
  1598.             (setq ange-ftp-xfer-size 0)
  1599.  
  1600.             ;; issue the "done" message since we've finished.
  1601.             (if (and ange-ftp-process-msg
  1602.                  ange-ftp-process-verbose
  1603.                  ange-ftp-process-result)
  1604.             (progn
  1605.               (ange-ftp-message "%s...done" ange-ftp-process-msg)
  1606.               (ange-ftp-repaint-minibuffer)
  1607.               (setq ange-ftp-process-msg nil)))
  1608.             
  1609.             ;; is there a continuation we should be calling?  if so,
  1610.             ;; we'd better call it, making sure we only call it once.
  1611.             (if ange-ftp-process-continue
  1612.             (let ((cont ange-ftp-process-continue))
  1613.               (setq ange-ftp-process-continue nil)
  1614.               (ange-ftp-call-cont cont
  1615.                           ange-ftp-process-result
  1616.                           ange-ftp-process-result-line))))))
  1617.       (set-buffer old-buffer)))))
  1618.  
  1619. (defun ange-ftp-process-sentinel (proc str)
  1620.   "When ftp process changes state, nuke all file-entries in cache."
  1621.   (ange-ftp-save-match-data
  1622.     (let ((name (process-name proc)))
  1623.       (if (string-match "\\*ftp \\([^@]+\\)@\\([^*]+\\)*" name)
  1624.       (let ((user (substring name (match-beginning 1) (match-end 1)))
  1625.         (host (substring name (match-beginning 2) (match-end 2))))
  1626.         (ange-ftp-wipe-file-entries host user))))
  1627.     (setq ange-ftp-ls-cache-file nil)))
  1628.  
  1629. ;;;; ------------------------------------------------------------
  1630. ;;;; Gateway support.
  1631. ;;;; ------------------------------------------------------------
  1632.  
  1633. (defun ange-ftp-use-gateway-p (host)
  1634.   "Returns whether to access this host via a normal (non-smart) gateway."
  1635.   ;; yes, I know that I could simplify the following expression, but it is
  1636.   ;; clearer (to me at least) this way.
  1637.   (and (not ange-ftp-smart-gateway)
  1638.        (ange-ftp-save-match-data
  1639.      (not (string-match ange-ftp-local-host-regexp host)))))
  1640.  
  1641. (defun ange-ftp-use-smart-gateway-p (host)
  1642.   "Returns whether to access this host via a smart gateway."
  1643.   (and ange-ftp-smart-gateway
  1644.        (ange-ftp-save-match-data
  1645.      (not (string-match ange-ftp-local-host-regexp host)))))
  1646.  
  1647.  
  1648. ;;; ------------------------------------------------------------
  1649. ;;; Temporary file location and deletion...
  1650. ;;; ------------------------------------------------------------
  1651.  
  1652. (defvar ange-ftp-tmp-name-files ())
  1653. (defvar ange-ftp-tmp-name-hashtable (ange-ftp-make-hashtable 10))
  1654. (defvar ange-ftp-pid nil)
  1655.  
  1656. (defun ange-ftp-get-pid ()
  1657.   "Half-hearted attempt to get the current process's id."
  1658.   (setq ange-ftp-pid (substring (make-temp-name "") 1)))
  1659.  
  1660. (defun ange-ftp-make-tmp-name (host)
  1661.   "This routine will return the name of a new file."
  1662.   (let* ((template (if (ange-ftp-use-gateway-p host)
  1663.                ange-ftp-gateway-tmp-name-template
  1664.              ange-ftp-tmp-name-template))
  1665.      (pid (or ange-ftp-pid (ange-ftp-get-pid)))
  1666.      (start ?a)
  1667.      file entry)
  1668.     (while 
  1669.     (progn
  1670.       (setq file (format "%s%c%s" template start pid))
  1671.       (setq entry (intern file ange-ftp-tmp-name-hashtable))
  1672.       (or (memq entry ange-ftp-tmp-name-files)
  1673.           (ange-ftp-real-file-exists-p file)))
  1674.       (if (> (setq start (1+ start)) ?z)
  1675.       (progn
  1676.         (setq template (concat template "X"))
  1677.         (setq start ?a))))
  1678.     (setq ange-ftp-tmp-name-files
  1679.       (cons entry ange-ftp-tmp-name-files))
  1680.     file))
  1681.  
  1682. (defun ange-ftp-del-tmp-name (temp)
  1683.   (setq ange-ftp-tmp-name-files
  1684.     (delq (intern temp ange-ftp-tmp-name-hashtable)
  1685.           ange-ftp-tmp-name-files))
  1686.   (condition-case ()
  1687.       (ange-ftp-real-delete-file temp)
  1688.     (error nil)))
  1689.  
  1690. ;;;; ------------------------------------------------------------
  1691. ;;;; Interactive gateway program support.
  1692. ;;;; ------------------------------------------------------------
  1693.  
  1694. (defvar ange-ftp-gwp-running t)
  1695. (defvar ange-ftp-gwp-status nil)
  1696.  
  1697. (defun ange-ftp-gwp-sentinel (proc str)
  1698.   (setq ange-ftp-gwp-running nil))
  1699.  
  1700. (defun ange-ftp-gwp-filter (proc str)
  1701.   (ange-ftp-save-match-data
  1702.     (ange-ftp-process-log-string proc str)
  1703.     (cond ((string-match "login: *$" str)
  1704.        (send-string proc
  1705.             (concat
  1706.              (let ((ange-ftp-default-user t))
  1707.                (ange-ftp-get-user ange-ftp-gateway-host))
  1708.              "\n")))
  1709.       ((string-match "Password: *$" str)
  1710.        (send-string proc
  1711.             (concat
  1712.              (ange-ftp-get-passwd ange-ftp-gateway-host
  1713.                           (ange-ftp-get-user
  1714.                            ange-ftp-gateway-host))
  1715.              "\n")))
  1716.       ((string-match ange-ftp-gateway-fatal-msgs str)
  1717.        (delete-process proc)
  1718.        (setq ange-ftp-gwp-running nil))
  1719.       ((string-match ange-ftp-gateway-prompt-pattern str)
  1720.        (setq ange-ftp-gwp-running nil
  1721.          ange-ftp-gwp-status t)))))
  1722.  
  1723. (defun ange-ftp-gwp-start (host user name args)
  1724.   "Login to the gateway machine and fire up an ftp process."
  1725.   (let* ((gw-user (ange-ftp-get-user ange-ftp-gateway-host))
  1726.      (proc (start-process name name 
  1727.                   ange-ftp-gateway-program
  1728.                   ange-ftp-gateway-host))
  1729.      (ftp (mapconcat (function identity) args " ")))
  1730.     (process-kill-without-query proc)
  1731.     (set-process-sentinel proc (function ange-ftp-gwp-sentinel))
  1732.     (set-process-filter proc (function ange-ftp-gwp-filter))
  1733.     (set-marker (process-mark proc) (point))
  1734.     (setq ange-ftp-gwp-running t
  1735.       ange-ftp-gwp-status nil)
  1736.     (ange-ftp-message "Connecting to gateway %s..." ange-ftp-gateway-host)
  1737.     (while ange-ftp-gwp-running        ;perform login sequence
  1738.       (accept-process-output proc))
  1739.     (if (not ange-ftp-gwp-status)
  1740.     (ange-ftp-error host user "unable to login to gateway"))
  1741.     (ange-ftp-message "Connecting to gateway %s...done" ange-ftp-gateway-host)
  1742.     (setq ange-ftp-gwp-running t
  1743.       ange-ftp-gwp-status nil)
  1744.     (process-send-string proc ange-ftp-gateway-setup-term-command)
  1745.     (while ange-ftp-gwp-running        ;zap ^M's and double echoing.
  1746.       (accept-process-output proc))
  1747.     (if (not ange-ftp-gwp-status)
  1748.     (ange-ftp-error host user "unable to set terminal modes on gateway"))
  1749.     (setq ange-ftp-gwp-running t
  1750.       ange-ftp-gwp-status nil)
  1751.     (process-send-string proc (concat "exec " ftp "\n")) ;spawn ftp process
  1752.     proc))
  1753.  
  1754. ;;;; ------------------------------------------------------------
  1755. ;;;; Support for sending commands to the ftp process.
  1756. ;;;; ------------------------------------------------------------
  1757.  
  1758. (defun ange-ftp-raw-send-cmd (proc cmd &optional msg cont nowait)
  1759.   "Low-level routine to send the given ftp CMD to the ftp PROCESS.
  1760. MSG is an optional message to output before and after the command.
  1761. If CONT is non-NIL then it is either a function or a list of function and
  1762. some arguments.  The function will be called when the ftp command has completed.
  1763. If CONT is NIL then this routine will return \( RESULT . LINE \) where RESULT
  1764. is whether the command was successful, and LINE is the line from the FTP
  1765. process that caused the command to complete.
  1766. If NOWAIT is given then the routine will return immediately the command has
  1767. been queued with no result.  CONT will still be called, however."
  1768.   (if (memq (process-status proc) '(run open))
  1769.       (save-excursion
  1770.     (set-buffer (process-buffer proc))
  1771.     (while ange-ftp-process-busy
  1772.       (accept-process-output))
  1773.     (setq ange-ftp-process-string ""
  1774.           ange-ftp-process-result-line ""
  1775.           ange-ftp-process-busy t
  1776.           ange-ftp-process-result nil
  1777.           ange-ftp-process-multi-skip nil
  1778.           ange-ftp-process-msg msg
  1779.           ange-ftp-process-continue cont
  1780.           ange-ftp-hash-mark-count 0
  1781.           ange-ftp-last-percent -1
  1782.           cmd (concat cmd "\n"))
  1783.     (and msg ange-ftp-process-verbose (ange-ftp-message "%s..." msg))
  1784.     (goto-char (point-max))
  1785. ;    (move-marker last-input-start (point))
  1786.     ;; don't insert the password into the buffer on the USER command.
  1787.     (ange-ftp-save-match-data
  1788.       (if (string-match "^user \"[^\"]*\"" cmd)
  1789.           (insert (substring cmd 0 (match-end 0)) " Turtle Power!\n")
  1790.         (insert cmd)))
  1791. ;    (move-marker last-input-end (point))
  1792.     (send-string proc cmd)
  1793.     (set-marker (process-mark proc) (point))
  1794.     (if nowait
  1795.         nil
  1796.       ;; hang around for command to complete
  1797.       (while ange-ftp-process-busy
  1798.         (accept-process-output proc))
  1799.       (if cont
  1800.           nil            ;cont has already been called
  1801.         (cons ange-ftp-process-result ange-ftp-process-result-line))))))
  1802.  
  1803. (defun ange-ftp-nslookup-host (host)
  1804.   "Attempt to resolve the given HOSTNAME using nslookup if possible."
  1805.   (interactive "sHost:  ")
  1806.   (if ange-ftp-nslookup-program
  1807.       (let ((proc (start-process " *nslookup*" " *nslookup*"
  1808.                  ange-ftp-nslookup-program host))
  1809.         (res host))
  1810.     (process-kill-without-query proc)
  1811.     (save-excursion
  1812.       (set-buffer (process-buffer proc))
  1813.       (while (memq (process-status proc) '(run open))
  1814.         (accept-process-output proc))
  1815.       (goto-char (point-min))
  1816.       (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t)
  1817.           (setq res (buffer-substring (match-beginning 1)
  1818.                       (match-end 1))))
  1819.       (kill-buffer (current-buffer)))
  1820.     res)
  1821.     host))
  1822.  
  1823. (defun ange-ftp-start-process (host user name)
  1824.   "Spawn a new ftp process ready to connect to machine HOST and give it NAME.
  1825. If HOST is only ftp-able through a gateway machine then spawn a shell
  1826. on the gateway machine to do the ftp instead."
  1827.   (let* ((use-gateway (ange-ftp-use-gateway-p host))
  1828.      (ftp-prog (if use-gateway 
  1829.                ange-ftp-gateway-ftp-program-name
  1830.              ange-ftp-ftp-program-name))
  1831.      (args (append (list ftp-prog) ange-ftp-ftp-program-args))
  1832.      proc)
  1833.     (if use-gateway
  1834.     (if ange-ftp-gateway-program-interactive
  1835.         (setq proc (ange-ftp-gwp-start host user name args))
  1836.       (setq proc (apply 'start-process name name
  1837.                 (append (list ange-ftp-gateway-program
  1838.                       ange-ftp-gateway-host)
  1839.                     args))))
  1840.       (setq proc (apply 'start-process name name args)))
  1841.     (process-kill-without-query proc)
  1842.     (set-process-sentinel proc (function ange-ftp-process-sentinel))
  1843.     (set-process-filter proc (function ange-ftp-process-filter))
  1844.     ;; jwz: turn on shell mode after setting the proc filter for the
  1845.     ;; benefit of shell-font.
  1846.     (require 'shell)
  1847.     (save-excursion
  1848.       (set-buffer (process-buffer proc))
  1849.       (ange-ftp-shell-mode))
  1850.     (accept-process-output proc)    ;wait for ftp startup message
  1851.     proc))
  1852.  
  1853. (defun ange-ftp-smart-login (host user pass account proc)
  1854.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1855. PROC is the FTP-client's process.  This routine uses the smart-gateway
  1856. host specified in ``ange-ftp-gateway-host''."
  1857.   (let ((result (ange-ftp-raw-send-cmd
  1858.          proc
  1859.          (format "open %s %s"
  1860.              (ange-ftp-nslookup-host ange-ftp-gateway-host)
  1861.              ange-ftp-smart-gateway-port)
  1862.          (format "Opening FTP connection to %s via %s"
  1863.              host
  1864.              ange-ftp-gateway-host))))
  1865.     (or (car result)
  1866.     (ange-ftp-error host user 
  1867.             (concat "OPEN request failed: "
  1868.                 (cdr result))))
  1869.     (setq result (ange-ftp-raw-send-cmd
  1870.           proc (format "user \"%s\"@%s %s %s"
  1871.                    user
  1872.                    (ange-ftp-nslookup-host host)
  1873.                    pass
  1874.                    account)
  1875.           (format "Logging in as user %s@%s"
  1876.               user host)))
  1877.     (or (car result)
  1878.     (progn
  1879.       (ange-ftp-set-passwd host user nil) ; reset password
  1880.       (ange-ftp-set-account host user nil) ; reset account
  1881.       (ange-ftp-error host user
  1882.               (concat "USER request failed: "
  1883.                   (cdr result)))))))
  1884.  
  1885. (defun ange-ftp-normal-login (host user pass account proc)
  1886.   "Connect to the FTP-server on HOST as USER using PASSWORD and ACCOUNT.
  1887. PROC is the process to the FTP-client."
  1888.   (let ((result (ange-ftp-raw-send-cmd
  1889.          proc
  1890.          (format "open %s" (ange-ftp-nslookup-host host))
  1891.          (format "Opening FTP connection to %s" host))))
  1892.     (or (car result)
  1893.     (ange-ftp-error host user
  1894.             (concat "OPEN request failed: "
  1895.                 (cdr result))))
  1896.     (setq result (ange-ftp-raw-send-cmd
  1897.           proc
  1898.           (format "user \"%s\" %s %s" user pass account)
  1899.           (format "Logging in as user %s@%s" user host)))
  1900.     (or (car result)
  1901.     (progn
  1902.       (ange-ftp-set-passwd host user nil) ;reset password.
  1903.       (ange-ftp-set-account host user nil) ;reset account.
  1904.       (ange-ftp-error host user
  1905.               (concat "USER request failed: "
  1906.                   (cdr result)))))))
  1907.  
  1908. (defvar ange-ftp-hash-mark-msgs
  1909.   "[hH]ash mark [^0-9]*\\([0-9]+\\)"
  1910.   "*Regexp matching the FTP client's output upon doing a HASH command.")
  1911.  
  1912. (defun ange-ftp-guess-hash-mark-size (proc)
  1913.   (if ange-ftp-send-hash
  1914.       (save-excursion
  1915.     (set-buffer (process-buffer proc))
  1916.     (let* ((status (ange-ftp-raw-send-cmd proc "hash"))
  1917.            (result (car status))
  1918.            (line (cdr status)))
  1919.       (ange-ftp-save-match-data
  1920.         (if (string-match ange-ftp-hash-mark-msgs line)
  1921.         (let ((size (string-to-int
  1922.                 (substring line
  1923.                        (match-beginning 1)
  1924.                        (match-end 1)))))
  1925.           (setq ange-ftp-ascii-hash-mark-size size
  1926.             ange-ftp-hash-mark-unit (ash size -4))
  1927.  
  1928.           ;; if a default value for this is set, use that value.
  1929.           (or ange-ftp-binary-hash-mark-size
  1930.               (setq ange-ftp-binary-hash-mark-size size)))))))))
  1931.  
  1932. (defun ange-ftp-get-process (host user)
  1933.   "Return the process object for a FTP process connected to HOST and
  1934. logged in as USER.  Create a new process if needed."
  1935.   (let* ((name (ange-ftp-ftp-process-buffer host user))
  1936.      (proc (get-process name)))
  1937.     (if (and proc (memq (process-status proc) '(run open)))
  1938.     proc
  1939.       (let ((pass (ange-ftp-quote-string
  1940.            (ange-ftp-get-passwd host user)))
  1941.         (account (ange-ftp-quote-string
  1942.               (ange-ftp-get-account host user))))
  1943.     ;; grab a suitable process.
  1944.     (setq proc (ange-ftp-start-process host user name))
  1945.     
  1946.     ;; login to FTP server.
  1947.     (if (ange-ftp-use-smart-gateway-p host)
  1948.         (ange-ftp-smart-login host user pass account proc)
  1949.       (ange-ftp-normal-login host user pass account proc))
  1950.       
  1951.     ;; Tell client to send back hash-marks as progress.  It isn't usually
  1952.     ;; fatal if this command fails.
  1953.     (ange-ftp-guess-hash-mark-size proc)
  1954.  
  1955.     ;; Guess at the host type.
  1956.     (ange-ftp-guess-host-type host user)
  1957.  
  1958.     ;; Run any user-specified hooks.  Note that proc, host and user are
  1959.     ;; dynamically bound at this point.
  1960.     (run-hooks 'ange-ftp-process-startup-hook))
  1961.       proc)))
  1962.  
  1963. ;; Variables for caching host and host-type
  1964. (defvar ange-ftp-host-cache nil)
  1965. (defvar ange-ftp-host-type-cache nil)
  1966.  
  1967. ;; If ange-ftp-host-type is called with the optional user
  1968. ;; argument, it will attempt to guess the host type by connecting
  1969. ;; as user, if necessary. For efficiency, I have tried to give this
  1970. ;; optional second argument only when necessary. Have I missed any calls
  1971. ;; to ange-ftp-host-type where it should have been supplied?
  1972.  
  1973. (defun ange-ftp-host-type (host &optional user)
  1974.   "Return a symbol which represents the type of the HOST given.
  1975. If the optional argument USER is given, attempts to guess the
  1976. host-type by logging in as USER."
  1977.   (if (eq host ange-ftp-host-cache)
  1978.       ange-ftp-host-type-cache
  1979.     ;; Trigger an ftp connection, in case we need to guess at the host type.
  1980.     (if (and user (ange-ftp-get-process host user) (eq host ange-ftp-host-cache))
  1981.     ange-ftp-host-type-cache
  1982.       (setq ange-ftp-host-cache host
  1983.         ange-ftp-host-type-cache
  1984.         (cond ((ange-ftp-dumb-unix-host host)
  1985.            'dumb-unix)
  1986.           ((and (fboundp 'ange-ftp-vos-host)
  1987.             (ange-ftp-vos-host host))
  1988.            'vos)
  1989.           ((and (fboundp 'ange-ftp-vms-host)
  1990.             (ange-ftp-vms-host host))
  1991.            'vms)
  1992.           ((and (fboundp 'ange-ftp-mts-host)
  1993.             (ange-ftp-mts-host host))
  1994.            'mts)
  1995.           ((and (fboundp 'ange-ftp-cms-host)
  1996.             (ange-ftp-cms-host host))
  1997.            'cms)
  1998.           (t
  1999.            'unix))))))
  2000.  
  2001. ;; It would be nice to abstract the functions ange-ftp-TYPE-host and
  2002. ;; ange-ftp-add-TYPE-host. The trick is to abstract these functions
  2003. ;; without sacrificing speed. Also, having separate variables
  2004. ;; ange-ftp-TYPE-regexp is more user friendly then requiring the user to
  2005. ;; set an alist to indicate that a host is of a given type. Even with
  2006. ;; automatic host type recognition, setting a regexp is still a good idea
  2007. ;; (for efficiency) if you log into a particular non-UNIX host frequently.
  2008.  
  2009. (defvar ange-ftp-fix-path-func-alist nil
  2010.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2011. which can change a UNIX path into a path more suitable for a host of type
  2012. TYPE.")
  2013.  
  2014. (defvar ange-ftp-fix-dir-path-func-alist nil
  2015.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is a routine
  2016. which can change UNIX directory path into a directory path more suitable
  2017. for a host of type TYPE.")
  2018.  
  2019. ;; *** Perhaps the sense of this variable should be inverted, since there
  2020. ;; *** is only 1 host type that can take ls-style listing options.
  2021. (defvar ange-ftp-dumb-host-types '(dumb-unix)
  2022.   "List of host types that can't take UNIX ls-style listing options.")
  2023.  
  2024. (defun ange-ftp-send-cmd (host user cmd &optional msg cont nowait)
  2025.   "Find an ftp process connected to HOST logged in as USER and send it CMD.
  2026. MSG is an optional status message to be output before and after issuing the
  2027. command.
  2028. See the documentation for ange-ftp-raw-send-cmd for a description of CONT
  2029. and NOWAIT."
  2030.   ;; Handle conversion to remote pathname syntax and remote ls option
  2031.   ;; capability.
  2032.   (let ((cmd0 (car cmd))
  2033.     (cmd1 (nth 1 cmd))
  2034.     cmd2 cmd3 host-type fix-pathname-func)
  2035.  
  2036.     (cond
  2037.      
  2038.      ;; pwd case (We don't care what host-type.)
  2039.      ((null cmd1))
  2040.      
  2041.      ;; cmd == 'dir "remote-path" "local-path" "ls-switches"
  2042.      ((progn
  2043.        (setq cmd2 (nth 2 cmd)
  2044.          host-type (ange-ftp-host-type host user))
  2045.        ;; This will trigger an FTP login, if one doesn't exist
  2046.        (eq cmd0 'dir))
  2047.       (setq cmd1 (funcall
  2048.           (or (cdr (assq host-type ange-ftp-fix-dir-path-func-alist))
  2049.               'identity)
  2050.           cmd1)
  2051.         cmd3 (nth 3 cmd))
  2052.       ;; Need to deal with the HP-UX ftp bug. This should also allow
  2053.       ;; us to resolve symlinks to directories on SysV machines. (Sebastian will
  2054.       ;; be happy.)
  2055.       (and (eq host-type 'unix)
  2056.        (string-match "/$" cmd1)
  2057.        (not (string-match "R" cmd3))
  2058.        (setq cmd1 (concat cmd1 ".")))
  2059.       ;; If the remote ls can take switches, put them in
  2060.       (or (memq host-type ange-ftp-dumb-host-types)
  2061.       (setq cmd0 'ls
  2062.         cmd1 (format "\"%s %s\"" cmd3 cmd1))))
  2063.      
  2064.      ;; First argument is the remote pathname
  2065.      ((progn
  2066.     (setq fix-pathname-func (or (cdr (assq host-type
  2067.                            ange-ftp-fix-path-func-alist))
  2068.                     'identity))
  2069.     (memq cmd0 '(get delete mkdir rmdir cd)))
  2070.       (setq cmd1 (funcall fix-pathname-func cmd1)))
  2071.  
  2072.      ;; Second argument is the remote pathname
  2073.      ((memq cmd0 '(append put chmod))
  2074.       (setq cmd2 (funcall fix-pathname-func cmd2)))
  2075.  
  2076.      ;; Both arguments are remote pathnames
  2077.      ((eq cmd0 'rename)
  2078.       (setq cmd1 (funcall fix-pathname-func cmd1)
  2079.         cmd2 (funcall fix-pathname-func cmd2))))
  2080.       
  2081.     ;; Turn the command into one long string 
  2082.     (setq cmd0 (symbol-name cmd0))
  2083.     (setq cmd (concat cmd0
  2084.               (and cmd1 (concat " " cmd1))
  2085.               (and cmd2 (concat " " cmd2))))
  2086.  
  2087.     ;; Actually send the resulting command.
  2088.     (let (afsc-result
  2089.       afsc-line)
  2090.       (ange-ftp-raw-send-cmd
  2091.        (ange-ftp-get-process host user)
  2092.        cmd
  2093.        msg
  2094.        (list
  2095.     (function (lambda (result line host user
  2096.                   cmd msg cont nowait)
  2097.             (or cont
  2098.             (setq afsc-result result
  2099.                   afsc-line line))
  2100.             (if result
  2101.             (ange-ftp-call-cont cont result line)
  2102.               (ange-ftp-raw-send-cmd
  2103.                (ange-ftp-get-process host user)
  2104.                cmd
  2105.                msg
  2106.                (list
  2107.             (function (lambda (result line cont)
  2108.                     (or cont
  2109.                     (setq afsc-result result
  2110.                           afsc-line line))
  2111.                     (ange-ftp-call-cont cont result line)))
  2112.             cont)
  2113.                nowait))))
  2114.     host user cmd msg cont nowait)
  2115.        nowait)
  2116.  
  2117.       (if nowait
  2118.       nil
  2119.     (if cont
  2120.         nil
  2121.       (cons afsc-result afsc-line))))))
  2122.  
  2123. ;; It might be nice to message users about the host type identified,
  2124. ;; but there is so much other messaging going on, it would not be
  2125. ;; seen. No point in slowing things down just so users can read
  2126. ;; a host type message.
  2127.  
  2128. (defconst ange-ftp-cms-path-template
  2129.   (concat
  2130.    "^[-A-Z0-9$*][-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?"
  2131.    "[-A-Z0-9$*]?[-A-Z0-9$*]?[-A-Z0-9$*]?\\.[0-9][0-9][0-9A-Z]$"))
  2132. (defconst ange-ftp-vms-path-template
  2133.   "^[-A-Z0-9_$]+:\\[[-A-Z0-9_$]+\\(\\.[-A-Z0-9_$]+\\)*\\]$")
  2134. (defconst ange-ftp-mts-path-template
  2135.   "^[A-Z0-9._][A-Z0-9._][A-Z0-9._][A-Z0-9._]:$")
  2136.  
  2137. (defun ange-ftp-guess-host-type (host user)
  2138.   "Guess at the the host type of HOST by doing a pwd, and examining
  2139. the directory syntax."
  2140.   (let ((host-type (ange-ftp-host-type host))
  2141.     (key (concat host "/" user "/~")))
  2142.     (if (eq host-type 'unix)
  2143.     ;; Note that ange-ftp-host-type returns unix as the default value.
  2144.     (ange-ftp-save-match-data
  2145.       (let* ((result (ange-ftp-get-pwd host user))
  2146.          (dir (car result))
  2147.          fix-path-func)
  2148.         (cond ((null dir)
  2149.            (message "Warning! Unable to get home directory")
  2150.            (sit-for 1)
  2151.            (if (string-match
  2152.             "^450 No current working directory defined$"
  2153.             (cdr result))
  2154.                
  2155.                ;; We'll assume that if pwd bombs with this
  2156.                ;; error message, then it's CMS.
  2157.                (progn
  2158.              (ange-ftp-add-cms-host host)
  2159.              (setq ange-ftp-host-cache host
  2160.                    ange-ftp-host-type-cache 'cms))))
  2161.  
  2162.           ;; try for VMS
  2163.           ((string-match ange-ftp-vms-path-template dir)
  2164.            (ange-ftp-add-vms-host host)
  2165.            ;; The add-host functions clear the host type cache.
  2166.            ;; Therefore, need to set the cache afterwards.
  2167.            (setq ange-ftp-host-cache host
  2168.              ange-ftp-host-type-cache 'vms))
  2169.  
  2170.           ;; try for MTS
  2171.           ((string-match ange-ftp-mts-path-template dir)
  2172.            (ange-ftp-add-mts-host host)
  2173.            (setq ange-ftp-host-cache host
  2174.              ange-ftp-host-type-cache 'mts))
  2175.           
  2176.           ;; try for CMS
  2177.           ((string-match ange-ftp-cms-path-template dir)
  2178.            (ange-ftp-add-cms-host host)
  2179.            (setq ange-ftp-host-cache host
  2180.              ange-ftp-host-type-cache 'cms))
  2181.  
  2182.           ;; assume UN*X
  2183.           (t
  2184.            (setq ange-ftp-host-cache host
  2185.              ange-ftp-host-type-cache 'unix)))
  2186.  
  2187.         ;; Now that we have done a pwd, might as well put it in
  2188.         ;; the expand-dir hashtable.
  2189.         (setq fix-path-func (cdr (assq ange-ftp-host-type-cache
  2190.                        ange-ftp-fix-path-func-alist)))
  2191.         (if fix-path-func
  2192.         (setq dir (funcall fix-path-func dir 'reverse)))
  2193.         (ange-ftp-put-hash-entry key dir
  2194.                      ange-ftp-expand-dir-hashtable))))
  2195.  
  2196.     ;; In the special case of CMS make sure that know the
  2197.     ;; expansion of the home minidisk now, because we will
  2198.     ;; be doing a lot of cd's.
  2199.     (if (and (eq host-type 'cms)
  2200.          (not (ange-ftp-hash-entry-exists-p
  2201.            key ange-ftp-expand-dir-hashtable)))
  2202.     (let ((dir (car (ange-ftp-get-pwd host user))))
  2203.       (if dir
  2204.           (ange-ftp-put-hash-entry key (concat "/" dir)
  2205.                        ange-ftp-expand-dir-hashtable)
  2206.         (message "Warning! Unable to get home directory")
  2207.         (sit-for 1))))))
  2208.  
  2209.  
  2210. ;;;; ------------------------------------------------------------
  2211. ;;;; Simple FTP process shell support.
  2212. ;;;; ------------------------------------------------------------
  2213.  
  2214. (defvar ange-ftp-shell-mode-map nil)
  2215.  
  2216. (defun ange-ftp-shell-mode ()
  2217.   "Major mode for interacting with an FTP process.
  2218. Return at end of buffer sends line as input.
  2219. Return not at end copies rest of line to end and sends it.
  2220.  
  2221. The following commands imitate the usual Unix interrupt and editing
  2222. control characters:
  2223. \\{ange-ftp-shell-mode-map}
  2224. Runs ange-ftp-shell-mode-hook if not nil."
  2225.   (interactive)
  2226.   (let ((proc (get-buffer-process (current-buffer))))
  2227.     (kill-all-local-variables)
  2228.     (shell-mode)
  2229.     (if (null ange-ftp-shell-mode-map)
  2230.     (progn
  2231.       (setq ange-ftp-shell-mode-map (make-sparse-keymap))
  2232.       (set-keymap-parent ange-ftp-shell-mode-map shell-mode-map)
  2233.       (set-keymap-name ange-ftp-shell-mode-map 'ange-ftp-shell-mode-map)))
  2234.     (use-local-map ange-ftp-shell-mode-map)
  2235.     (setq major-mode 'ange-ftp-shell-mode)
  2236.     (setq mode-name "ange-ftp")
  2237.     (goto-char (point-max))
  2238.     (set-marker (process-mark proc) (point))
  2239.     (set (make-local-variable 'ange-ftp-process-string) nil)
  2240.     (setq ange-ftp-process-string "")
  2241.     (set (make-local-variable 'ange-ftp-process-busy) nil)
  2242.     (set (make-local-variable 'ange-ftp-process-result) nil)
  2243.     (set (make-local-variable 'ange-ftp-process-msg) nil)
  2244.     (set (make-local-variable 'ange-ftp-process-multi-skip) nil)
  2245.     (set (make-local-variable 'ange-ftp-process-result-line) nil)
  2246.     (set (make-local-variable 'ange-ftp-process-continue) nil)
  2247.     (set (make-local-variable 'ange-ftp-hash-mark-count) nil)
  2248.     (set (make-local-variable 'ange-ftp-binary-hash-mark-size) nil)
  2249.     (set (make-local-variable 'ange-ftp-ascii-hash-mark-size) nil)
  2250.     (set (make-local-variable 'ange-ftp-hash-mark-unit) nil)
  2251.     (set (make-local-variable 'ange-ftp-xfer-size) nil)
  2252.     (set (make-local-variable 'ange-ftp-last-percent) nil)
  2253.     (setq ange-ftp-hash-mark-count 0)
  2254.     (setq ange-ftp-xfer-size 0)
  2255.     (setq ange-ftp-process-result-line "")
  2256.     (run-hooks 'ange-ftp-shell-mode-hook)))
  2257.  
  2258. ;;;; ------------------------------------------------------------
  2259. ;;;; Remote file and directory listing support.
  2260. ;;;; ------------------------------------------------------------
  2261.  
  2262. (defun ange-ftp-dumb-unix-host (host)
  2263.   "Returns whether HOST's FTP server doesn't like \'ls\' or \'dir\' commands
  2264. to take switch arguments."
  2265.   (and ange-ftp-dumb-unix-host-regexp
  2266.        (ange-ftp-save-match-data
  2267.      (string-match ange-ftp-dumb-unix-host-regexp host))))
  2268.  
  2269. (defun ange-ftp-add-dumb-unix-host (host)
  2270.   "Interactively adds a given HOST to ange-ftp-dumb-unix-host-regexp."
  2271.   (interactive
  2272.    (list (read-string "Host: "
  2273.               (let ((name (or (buffer-file-name)
  2274.                       (and (eq major-mode 'dired-mode)
  2275.                        dired-directory))))
  2276.             (and name (car (ange-ftp-ftp-path name)))))))
  2277.   (if (not (ange-ftp-dumb-unix-host host))
  2278.       (setq ange-ftp-dumb-unix-host-regexp
  2279.         (concat "^" (regexp-quote host) "$"
  2280.             (and ange-ftp-dumb-unix-host-regexp "\\|")
  2281.             ange-ftp-dumb-unix-host-regexp)
  2282.         ange-ftp-host-cache nil)))
  2283.  
  2284. (defvar ange-ftp-parse-list-func-alist nil
  2285.   "Association list of \( TYPE \. FUNC \) pairs.  The FUNC is a routine
  2286. which can parse the output from a DIR listing for a host of type TYPE.")
  2287.  
  2288. ;; With no-error nil, this function returns:
  2289. ;; an error if file is not an ange-ftp-path
  2290. ;;                      (This should never happen.) 
  2291. ;; an error if either the listing is unreadable or there is an ftp error.
  2292. ;; the listing (a string), if everything works.
  2293. ;; 
  2294. ;; With no-error t, it returns:
  2295. ;; an error if not an ange-ftp-path
  2296. ;; error if listing is unreable (most likely caused by a slow connection)
  2297. ;; nil if ftp error (this is because although asking to list a nonexistent
  2298. ;;                   directory on a remote unix machine usually (except
  2299. ;;                   maybe for dumb hosts) returns an ls error, but no
  2300. ;;                   ftp error, if the same is done on a VMS machine,
  2301. ;;                   an ftp error is returned. Need to trap the error
  2302. ;;                   so we can go on and try to list the parent.)
  2303. ;; the listing, if everything works.
  2304.  
  2305. (defun ange-ftp-ls (file lsargs parse &optional no-error)
  2306.   "Return the output of an `DIR' or `ls' command done over ftp.
  2307. FILE is the full name of the remote file, LSARGS is any args to pass to the
  2308. `ls' command, and PARSE specifies that the output should be parsed and stored
  2309. away in the internal cache."
  2310.   ;; If parse is t, we assume that file is a directory. i.e. we only parse
  2311.   ;; full directory listings.
  2312.   (setq file (ange-ftp-expand-file-name file))
  2313.   (let ((parsed (ange-ftp-ftp-path file)))
  2314.     (if parsed
  2315.     (let* ((host (nth 0 parsed))
  2316.            (user (nth 1 parsed))
  2317.            (path (ange-ftp-quote-string (nth 2 parsed)))
  2318.            (key (directory-file-name file))
  2319.            (host-type (ange-ftp-host-type host user))
  2320.            (dumb (memq host-type ange-ftp-dumb-host-types))
  2321.            result
  2322.            temp
  2323.            lscmd parse-func)
  2324.       (if (string-equal path "")
  2325.           (setq path
  2326.             (ange-ftp-real-file-name-as-directory
  2327.                   (ange-ftp-expand-dir host user "~"))))
  2328.       (if (and ange-ftp-ls-cache-file
  2329.            (string-equal key ange-ftp-ls-cache-file)
  2330.            ;; Don't care about lsargs for dumb hosts.
  2331.            (or dumb (string-equal lsargs ange-ftp-ls-cache-lsargs)))
  2332.           ange-ftp-ls-cache-res
  2333.         (setq temp (ange-ftp-make-tmp-name host))
  2334.         (setq lscmd (list 'dir path temp lsargs))
  2335.         (unwind-protect
  2336.         (if (car (setq result (ange-ftp-send-cmd
  2337.                        host
  2338.                        user
  2339.                        lscmd
  2340.                        (format "Listing %s"
  2341.                            (ange-ftp-abbreviate-filename
  2342.                         file)))))
  2343.             (save-excursion
  2344.               (set-buffer (get-buffer-create
  2345.                    ange-ftp-data-buffer-name))
  2346.               (erase-buffer)
  2347.               (if (ange-ftp-real-file-readable-p temp)
  2348.               (ange-ftp-real-insert-file-contents temp)
  2349.             (sleep-for ange-ftp-retry-time)
  2350.                     ;wait for file to possibly appear
  2351.             (if (ange-ftp-real-file-readable-p temp)
  2352.                 ;; Try again.
  2353.                 (ange-ftp-real-insert-file-contents temp)
  2354.               (ange-ftp-error host user
  2355.                       (format
  2356.                        "list data file %s not readable"
  2357.                        temp))))
  2358.               (if parse
  2359.               (ange-ftp-set-files
  2360.                file
  2361.                (if (setq
  2362.                 parse-func
  2363.                 (cdr (assq host-type
  2364.                        ange-ftp-parse-list-func-alist)))
  2365.                    (funcall parse-func)
  2366.                  (ange-ftp-parse-dired-listing lsargs))))
  2367.               (setq ange-ftp-ls-cache-file key
  2368.                 ange-ftp-ls-cache-lsargs lsargs
  2369.                     ; For dumb hosts-types this is
  2370.                     ; meaningless but harmless.
  2371.                 ange-ftp-ls-cache-res (buffer-string))
  2372.               ;; (kill-buffer (current-buffer))
  2373.               ange-ftp-ls-cache-res)
  2374.           (if no-error
  2375.               nil
  2376.             (ange-ftp-error host user
  2377.                     (concat "DIR failed: " (cdr result)))))
  2378.           (ange-ftp-del-tmp-name temp))))
  2379.       (error "Should never happen. Please report. Bug ref. no.: 1"))))
  2380.  
  2381. ;;;; ------------------------------------------------------------
  2382. ;;;; Directory information caching support.
  2383. ;;;; ------------------------------------------------------------
  2384.  
  2385. (defconst ange-ftp-date-regexp
  2386.   (concat
  2387.    " \\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct"
  2388.    "\\|Nov\\|Dec\\) +[0-3]?[0-9] "))
  2389.  
  2390. (defvar ange-ftp-add-file-entry-alist nil
  2391.   "Association list of pairs \( TYPE \. FUNC \), where FUNC
  2392. is a function to be used to add a file entry for the OS TYPE. The
  2393. main reason for this alist is to deal with file versions in VMS.")
  2394.  
  2395. (defvar ange-ftp-delete-file-entry-alist nil
  2396.   "Association list of pairs \( TYPE \. FUNC \), where FUNC
  2397. is a function to be used to delete a file entry for the OS TYPE.
  2398. The main reason for this alist is to deal with file versions in
  2399. VMS.")
  2400.  
  2401. (defun ange-ftp-add-file-entry (path &optional dir-p)
  2402.   "Given a PATH, add the file entry for it, if its directory
  2403. info exists."
  2404.   (funcall (or (cdr (assq (ange-ftp-host-type
  2405.                (car (ange-ftp-ftp-path path)))
  2406.               ange-ftp-add-file-entry-alist))
  2407.            'ange-ftp-internal-add-file-entry)
  2408.        path dir-p)
  2409.   (setq ange-ftp-ls-cache-file nil))
  2410.  
  2411. (defun ange-ftp-delete-file-entry (path &optional dir-p)
  2412.   "Given a PATH, delete the file entry for it, if its directory
  2413. info exists."
  2414.   (funcall (or (cdr (assq (ange-ftp-host-type
  2415.                (car (ange-ftp-ftp-path path)))
  2416.               ange-ftp-delete-file-entry-alist))
  2417.            'ange-ftp-internal-delete-file-entry)
  2418.        path dir-p)
  2419.   (setq ange-ftp-ls-cache-file nil))
  2420.  
  2421. (defmacro ange-ftp-parse-filename ()
  2422.   ;;Extract the filename from the current line of a dired-like listing.
  2423.   (` (let ((eol (progn (end-of-line) (point))))
  2424.        (beginning-of-line)
  2425.        (if (re-search-forward ange-ftp-date-regexp eol t)
  2426.        (progn
  2427.          (skip-chars-forward " ")
  2428.          (skip-chars-forward "^ " eol)
  2429.          (skip-chars-forward " " eol)
  2430.          ;; We bomb on filenames starting with a space.
  2431.          (buffer-substring (point) eol))))))
  2432.   
  2433. ;; This deals with the F switch. Should also do something about
  2434. ;; unquoting names obtained with the SysV b switch and the GNU Q
  2435. ;; switch. See Sebastian's dired-get-filename.
  2436.  
  2437. (defmacro ange-ftp-ls-parser ()
  2438.   ;; Note that switches is dynamically bound.
  2439.   ;; Meant to be called by ange-ftp-parse-dired-listing
  2440.   (` (let ((tbl (ange-ftp-make-hashtable))
  2441.        (used-F (and (stringp switches)
  2442.             (string-match "F" switches)))
  2443.        file-type symlink directory file)
  2444.        (while (setq file (ange-ftp-parse-filename))
  2445.      (beginning-of-line)
  2446.      (skip-chars-forward "\t 0-9")
  2447.      (setq file-type (following-char)
  2448.            directory (eq file-type ?d))
  2449.      (if (eq file-type ?l)
  2450.          (if (string-match " -> " file)
  2451.          (setq symlink (substring file (match-end 0))
  2452.                file (substring file 0 (match-beginning 0)))
  2453.            ;; Shouldn't happen
  2454.            (setq symlink ""))
  2455.        (setq symlink nil))
  2456.      ;; Only do a costly regexp search if the F switch was used.
  2457.      (if (and used-F
  2458.           (not (string-equal file ""))
  2459.           (looking-at
  2460.            ".[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"))
  2461.          (let ((socket (eq file-type ?s))
  2462.            (executable
  2463.             (and (not symlink) ; x bits don't mean a thing for symlinks
  2464.              (string-match "[xst]"
  2465.                        (concat
  2466.                     (buffer-substring
  2467.                      (match-beginning 1)
  2468.                      (match-end 1))
  2469.                     (buffer-substring
  2470.                      (match-beginning 2)
  2471.                      (match-end 2))
  2472.                     (buffer-substring
  2473.                      (match-beginning 3)
  2474.                      (match-end 3)))))))
  2475.            ;; Some ls's with the F switch mark symlinks with an @ (ULTRIX)
  2476.            ;; and others don't. (sigh...) Beware, that some Unix's don't
  2477.            ;; seem to believe in the F-switch
  2478.            (if (or (and symlink (string-match "@$" file))
  2479.                (and directory (string-match "/$" file))
  2480.                (and executable (string-match "*$" file))
  2481.                (and socket (string-match "=$" file)))
  2482.            (setq file (substring file 0 -1)))))
  2483.      (ange-ftp-put-hash-entry file (or symlink directory) tbl)
  2484.      (forward-line 1))
  2485.        (ange-ftp-put-hash-entry "." t tbl)
  2486.        (ange-ftp-put-hash-entry ".." t tbl)
  2487.        tbl)))
  2488.  
  2489. ;;; The dl stuff for descriptive listings
  2490.  
  2491. (defvar ange-ftp-dl-dir-regexp nil
  2492.   "Regexp matching directories which are listed in dl format. This regexp
  2493. shouldn't be anchored with a trailing $ so that it will match subdirectories
  2494. as well.")
  2495.  
  2496. (defun ange-ftp-add-dl-dir (dir)
  2497.   "Interactively adds a given directory to ange-ftp-dl-dir-regexp."
  2498.   (interactive
  2499.    (list (read-string "Directory: "
  2500.               (let ((name (or (buffer-file-name)
  2501.                       (and (eq major-mode 'dired-mode)
  2502.                        dired-directory))))
  2503.             (and name (ange-ftp-ftp-path name)
  2504.                  (file-name-directory name))))))
  2505.   (if (not (and ange-ftp-dl-dir-regexp
  2506.         (string-match ange-ftp-dl-dir-regexp dir)))
  2507.       (setq ange-ftp-dl-dir-regexp
  2508.         (concat "^" (regexp-quote dir)
  2509.             (and ange-ftp-dl-dir-regexp "\\|")
  2510.             ange-ftp-dl-dir-regexp))))
  2511.  
  2512. (defmacro ange-ftp-dl-parser ()
  2513.   ;; Parse the current buffer, which is assumed to be a descriptive
  2514.   ;; listing, and return a hashtable.
  2515.   (` (let ((tbl (ange-ftp-make-hashtable)))
  2516.        (while (not (eobp))
  2517.      (ange-ftp-put-hash-entry
  2518.       (buffer-substring (point)
  2519.                 (progn
  2520.                   (skip-chars-forward "^ /\n")
  2521.                   (point)))
  2522.       (eq (following-char) ?/)
  2523.       tbl)
  2524.      (forward-line 1))
  2525.        (ange-ftp-put-hash-entry "." t tbl)
  2526.        (ange-ftp-put-hash-entry ".." t tbl)
  2527.        tbl)))
  2528.  
  2529. (defun ange-ftp-parse-dired-listing (&optional switches)
  2530.   "Parse the current buffer which is assumed to be in a dired-like listing
  2531. format, and return a hashtable as the result. If the listing is not really
  2532. a listing, then return nil."
  2533.   (ange-ftp-save-match-data
  2534.     (cond
  2535.      ((looking-at "^total [0-9]+$")
  2536.       (forward-line 1)
  2537.       (ange-ftp-ls-parser))
  2538.      ((looking-at "[^\n]+\\( not found\\|: Not a directory\\)\n\\'")
  2539.       ;; It's an ls error message.
  2540.       nil)
  2541.      ((eobp) ; i.e. (zerop (buffer-size))
  2542.       ;; This could be one of:
  2543.       ;; (1) An Ultrix ls error message
  2544.       ;; (2) A listing with the A switch of an empty directory
  2545.       ;;     on a machine which doesn't give a total line.
  2546.       ;; (3) The twilight zone.
  2547.       ;; We'll assume (1) for now.
  2548.       nil)
  2549.      ((re-search-forward ange-ftp-date-regexp nil t)
  2550.       (beginning-of-line)
  2551.       (ange-ftp-ls-parser))
  2552.      ((re-search-forward "^[^ \n\t]+ +\\([0-9]+\\|-\\|=\\) " nil t)
  2553.       ;; It's a dl listing (I hope).
  2554.       ;; file is bound by the call to ange-ftp-ls
  2555.       (ange-ftp-add-dl-dir file)
  2556.       (beginning-of-line)
  2557.       (ange-ftp-dl-parser))
  2558.      (t nil))))
  2559.  
  2560. (defun ange-ftp-set-files (directory files)
  2561.   "For a given DIRECTORY, set or change the associated FILES hashtable."
  2562.   (and files (ange-ftp-put-hash-entry (file-name-as-directory directory)
  2563.                       files ange-ftp-files-hashtable)))
  2564.  
  2565. (defun ange-ftp-get-files (directory &optional no-error)
  2566.   "Given a given DIRECTORY, return a hashtable of file entries.
  2567. This will give an error or return nil, depending on the value of
  2568. NO-ERROR, if a listing for DIRECTORY cannot be obtained."
  2569.   (setq directory (file-name-as-directory directory)) ;normalize
  2570.   (or (ange-ftp-get-hash-entry directory ange-ftp-files-hashtable)
  2571.       (ange-ftp-save-match-data
  2572.     (and (ange-ftp-ls directory
  2573.               ;; This is an efficiency hack. We try to
  2574.               ;; anticipate what sort of listing dired
  2575.               ;; might want, and cache just such a listing.
  2576.               (if (and (boundp 'dired-actual-switches)
  2577.                    (stringp dired-actual-switches)
  2578.                    ;; We allow the A switch, which lists
  2579.                    ;; all files except "." and "..".
  2580.                    ;; This is OK because we manually
  2581.                    ;; insert these entries
  2582.                    ;; in the hash table.
  2583.                    (string-match
  2584.                     "[aA]" dired-actual-switches)
  2585.                    (string-match
  2586.                     "l" dired-actual-switches)
  2587.                    (not (string-match
  2588.                      "R" dired-actual-switches)))
  2589.                   dired-actual-switches
  2590.                 (if (and (boundp 'dired-listing-switches)
  2591.                      (stringp dired-listing-switches)
  2592.                      (string-match
  2593.                       "[aA]" dired-listing-switches)
  2594.                      (string-match
  2595.                       "l" dired-listing-switches)
  2596.                      (not (string-match
  2597.                        "R" dired-listing-switches)))
  2598.                 dired-listing-switches
  2599.                   "-al"))
  2600.               t no-error)
  2601.          (ange-ftp-get-hash-entry
  2602.           directory ange-ftp-files-hashtable)))))
  2603.  
  2604. (defmacro ange-ftp-get-file-part (path)
  2605.   "Given PATH, return the file part that can be used for looking up the
  2606. file's entry in a hashtable."
  2607.   (` (let ((file (file-name-nondirectory (, path))))
  2608.        (if (string-equal file "")
  2609.        "."
  2610.      file))))
  2611.  
  2612. (defmacro ange-ftp-allow-child-lookup (dir file)
  2613.   "Return whether ange-ftp-file-entry-p and ange-ftp-get-file-entry are
  2614. allowed to determine if PATH is a sub-directory by listing it directly,
  2615. rather than listing its parent directory. This is used for efficiency so
  2616. that a wasted listing is not done:
  2617. 1. When looking for a .dired file in dired-x.el.
  2618. 2. The syntax of FILE and DIR make it impossible that FILE could be a valid
  2619.     subdirectory. This is of course an OS dependent judgement."
  2620.   (` (not
  2621.       (let* ((efile (, file)) ; expand once.
  2622.          (edir (, dir))
  2623.          (parsed (ange-ftp-ftp-path edir))
  2624.          (host-type (ange-ftp-host-type
  2625.              (car parsed))))
  2626.     (or
  2627.      ;; Deal with dired
  2628.      (and (boundp 'dired-local-variables-file)
  2629.           (stringp dired-local-variables-file)
  2630.           (string-equal dired-local-variables-file efile))
  2631.      ;; No dots in dir names in vms.
  2632.      (and (eq host-type 'vms)
  2633.           (string-match "\\." efile))
  2634.      ;; No subdirs in mts of cms.
  2635.      (and (memq host-type '(mts cms))
  2636.           (not (string-equal "/" (nth 2 parsed)))))))))
  2637.  
  2638. (defun ange-ftp-file-entry-p (path)
  2639.   "Given PATH, return whether there is a file entry for it."
  2640.   (let* ((path (directory-file-name path))
  2641.      (dir (file-name-directory path))
  2642.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2643.      (file (ange-ftp-get-file-part path)))
  2644.     (if ent
  2645.     (ange-ftp-hash-entry-exists-p file ent)
  2646.       (or (and (ange-ftp-allow-child-lookup dir file)
  2647.            (setq ent (ange-ftp-get-files path t))
  2648.            ;; Try a child lookup. i.e. try to list file as a
  2649.            ;; subdirectory of dir. This is a good idea because
  2650.            ;; we may not have read permission for file's parent. Also,
  2651.            ;; people tend to work down directory trees anyway. We use
  2652.            ;; no-error ;; because if file does not exist as a subdir.,
  2653.            ;; then dumb hosts will give an ftp error. Smart unix hosts
  2654.            ;; will simply send back the ls
  2655.            ;; error message.
  2656.            (ange-ftp-get-hash-entry "." ent))
  2657.       ;; Child lookup failed. Try the parent. If this bombs,
  2658.       ;; we are at wits end -- signal an error.
  2659.       ;; Problem: If this signals an error, the error message
  2660.       ;; may  not have a lot to do with what went wrong.
  2661.       (ange-ftp-hash-entry-exists-p file
  2662.                     (ange-ftp-get-files dir))))))
  2663.  
  2664. (defun ange-ftp-get-file-entry (path)
  2665.   "Given PATH, return the given file entry which will be either t for a
  2666. directory, nil for a normal file, or a string for a symlink. If the file
  2667. isn't in the hashtable, this also returns nil."
  2668.   (let* ((path (directory-file-name path))
  2669.      (dir (file-name-directory path))
  2670.      (ent (ange-ftp-get-hash-entry dir ange-ftp-files-hashtable))
  2671.      (file (ange-ftp-get-file-part path)))
  2672.     (if ent
  2673.     (ange-ftp-get-hash-entry file ent)
  2674.       (or (and (ange-ftp-allow-child-lookup dir file)
  2675.            (setq ent (ange-ftp-get-files path t))
  2676.            (ange-ftp-get-hash-entry "." ent))
  2677.            ;; i.e. it's a directory by child lookup
  2678.       (ange-ftp-get-hash-entry file
  2679.                    (ange-ftp-get-files dir))))))
  2680.  
  2681. (defun ange-ftp-internal-delete-file-entry (path &optional dir-p)
  2682.   (if dir-p
  2683.       (progn 
  2684.     (setq path (file-name-as-directory path))
  2685.     (ange-ftp-del-hash-entry path ange-ftp-files-hashtable)
  2686.     (setq path (directory-file-name path))))
  2687.   ;; Note that file-name-as-directory followed by directory-file-name
  2688.   ;; serves to canonicalize directory file names to their unix form.
  2689.   ;; i.e. in VMS, FOO.DIR -> FOO/ -> FOO
  2690.   (let ((files (ange-ftp-get-hash-entry (file-name-directory path)
  2691.                     ange-ftp-files-hashtable)))
  2692.     (if files
  2693.     (ange-ftp-del-hash-entry (ange-ftp-get-file-part path)
  2694.                  files))))
  2695.  
  2696. (defun ange-ftp-internal-add-file-entry (path &optional dir-p)
  2697.   (and dir-p
  2698.        (setq path (directory-file-name path)))
  2699.   (let ((files (ange-ftp-get-hash-entry (file-name-directory path)
  2700.                     ange-ftp-files-hashtable)))
  2701.     (if files
  2702.     (ange-ftp-put-hash-entry (ange-ftp-get-file-part path)
  2703.                  dir-p
  2704.                  files))))
  2705.  
  2706. (defun ange-ftp-wipe-file-entries (host user)
  2707.   "Replace the file entry information hashtable with one that doesn't have any
  2708. entries for the given HOST, USER pair."
  2709.   (let ((new-tbl (ange-ftp-make-hashtable (length ange-ftp-files-hashtable))))
  2710.     (ange-ftp-map-hashtable
  2711.      (function
  2712.       (lambda (key val)
  2713.     (let ((parsed (ange-ftp-ftp-path key)))
  2714.       (if parsed
  2715.           (let ((h (nth 0 parsed))
  2716.             (u (nth 1 parsed)))
  2717.         (or (and (equal host h) (equal user u))
  2718.             (ange-ftp-put-hash-entry key val new-tbl)))))))
  2719.      ange-ftp-files-hashtable)
  2720.     (setq ange-ftp-files-hashtable new-tbl)))
  2721.  
  2722. ;;;; ------------------------------------------------------------
  2723. ;;;; File transfer mode support.
  2724. ;;;; ------------------------------------------------------------
  2725.  
  2726. (defun ange-ftp-set-binary-mode (host user)
  2727.   "Tell the ftp process for the given HOST & USER to switch to binary mode."
  2728.   (let ((result (ange-ftp-send-cmd host user '(type "binary"))))
  2729.     (if (not (car result))
  2730.     (ange-ftp-error host user (concat "BINARY failed: " (cdr result)))
  2731.       (save-excursion
  2732.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2733.     (and ange-ftp-binary-hash-mark-size
  2734.          (setq ange-ftp-hash-mark-unit (ash ange-ftp-binary-hash-mark-size -4)))))))
  2735.  
  2736. (defun ange-ftp-set-ascii-mode (host user)
  2737.   "Tell the ftp process for the given HOST & USER to switch to ascii mode."
  2738.   (let ((result (ange-ftp-send-cmd host user '(type "ascii"))))
  2739.     (if (not (car result))
  2740.     (ange-ftp-error host user (concat "ASCII failed: " (cdr result)))
  2741.       (save-excursion
  2742.     (set-buffer (process-buffer (ange-ftp-get-process host user)))
  2743.     (and ange-ftp-ascii-hash-mark-size
  2744.          (setq ange-ftp-hash-mark-unit (ash ange-ftp-ascii-hash-mark-size -4)))))))
  2745.  
  2746. ;;; ------------------------------------------------------------
  2747. ;;; expand-file-name and friends...
  2748. ;;; ------------------------------------------------------------
  2749.  
  2750. (defun ange-ftp-cd (host user dir)
  2751.   (let ((result (ange-ftp-send-cmd host user (list 'cd dir) "Doing CD")))
  2752.     (or (car result)
  2753.     (ange-ftp-error host user (concat "CD failed: " (cdr result))))))
  2754.  
  2755. (defun ange-ftp-get-pwd (host user)
  2756.   "Attempts to get the current working directory for the given HOST/USER pair.
  2757. Returns \( DIR . LINE \) where DIR is either the directory or NIL if not found,
  2758. and LINE is the relevant success or fail line from the FTP-client."
  2759.   (let* ((result (ange-ftp-send-cmd host user '(pwd) "Getting PWD"))
  2760.      (line (cdr result))
  2761.      dir)
  2762.     (if (car result)
  2763.     (ange-ftp-save-match-data
  2764.       (and (or (string-match "\"\\([^\"]*\\)\"" line)
  2765.            (string-match " \\([^ ]+\\) " line)) ; stone-age VMS servers!
  2766.            (setq dir (substring line
  2767.                     (match-beginning 1)
  2768.                     (match-end 1))))))
  2769.     (cons dir line)))
  2770.  
  2771. (defconst ange-ftp-expand-dir-hashtable (ange-ftp-make-hashtable))
  2772.  
  2773. (defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")
  2774.  
  2775. (defun ange-ftp-expand-dir (host user dir)
  2776.   "Return the result of doing a PWD in the current FTP session to machine HOST
  2777. logged in as user USER and cd'd to directory DIR."
  2778.   (let* ((host-type (ange-ftp-host-type host user))
  2779.      ;; It is more efficient to call ange-ftp-host-type
  2780.      ;; before binding res, because ange-ftp-host-type sometimes
  2781.      ;; adds to the info in the expand-dir-hashtable.
  2782.      (fix-pathname-func
  2783.       (cdr (assq host-type ange-ftp-fix-path-func-alist)))
  2784.      (key (concat host "/" user "/" dir))
  2785.      (res (ange-ftp-get-hash-entry key ange-ftp-expand-dir-hashtable)))
  2786.     (or res
  2787.     (progn
  2788.       (or
  2789.        (string-equal user "anonymous")
  2790.        (string-equal user "ftp")
  2791.        (not (eq host-type 'unix))
  2792.        (let* ((ange-ftp-good-msgs (concat ange-ftp-expand-dir-regexp
  2793.                           "\\|"
  2794.                           ange-ftp-good-msgs))
  2795.           (result (ange-ftp-send-cmd host user
  2796.                          (list 'get dir "/dev/null")
  2797.                          (format "expanding %s" dir)))
  2798.           (line (cdr result)))
  2799.          (setq res
  2800.            (if (string-match ange-ftp-expand-dir-regexp line)
  2801.                (substring line 
  2802.                   (match-beginning 1)
  2803.                   (match-end 1))))))
  2804.       (or res
  2805.           (if (string-equal dir "~")
  2806.           (setq res (car (ange-ftp-get-pwd host user)))
  2807.         (let ((home (ange-ftp-expand-dir host user "~")))
  2808.           (unwind-protect
  2809.               (and (ange-ftp-cd host user dir)
  2810.                (setq res (car (ange-ftp-get-pwd host user))))
  2811.             (ange-ftp-cd host user home)))))
  2812.       (if res
  2813.           (progn
  2814.         (if fix-pathname-func
  2815.             (setq res (funcall fix-pathname-func res 'reverse)))
  2816.         (ange-ftp-put-hash-entry
  2817.          key res ange-ftp-expand-dir-hashtable)))
  2818.       res))))
  2819.  
  2820. (defun ange-ftp-canonize-filename (n)
  2821.   "Take a string and short-circuit //, /. and /.."
  2822.   (if (string-match "[^:]+//" n)        ;don't upset Apollo users
  2823.       (setq n (substring n (1- (match-end 0)))))
  2824.   (let ((parsed (ange-ftp-ftp-path n)))
  2825.     (if parsed
  2826.     (let ((host (car parsed))
  2827.           (user (nth 1 parsed))
  2828.           (path (nth 2 parsed)))
  2829.       
  2830.       ;; See if remote path is absolute.  If so then just expand it and
  2831.       ;; replace the path component of the overall path.
  2832.       (cond ((string-match "^/" path)
  2833.          path)
  2834.         
  2835.         ;; Path starts with ~ or ~user.  Resolve that part of the path
  2836.         ;; making it absolute then re-expand it.
  2837.         ((string-match "^~[^/]*" path)
  2838.          (let* ((tilda (substring path
  2839.                       (match-beginning 0) 
  2840.                       (match-end 0)))
  2841.             (rest (substring path (match-end 0)))
  2842.             (dir (ange-ftp-expand-dir host user tilda)))
  2843.            (if dir
  2844.                (setq path (concat dir rest))
  2845.              (error "User \"%s\" is not known" 
  2846.                 (substring tilda 1)))))
  2847.         
  2848.         ;; relative path.  Tack on homedir and re-expand.
  2849.         (t
  2850.          (let ((dir (ange-ftp-expand-dir host user "~")))
  2851.            (if dir
  2852.                (setq path (concat
  2853.                    (ange-ftp-real-file-name-as-directory dir)
  2854.                    path))
  2855.              (error "Unable to obtain CWD")))))
  2856.       
  2857.       (if (not (string-match "^//" path))
  2858.           (progn
  2859.         (setq path (ange-ftp-real-expand-file-name path))
  2860.  
  2861.         (if (string-match "^//" path)
  2862.             (setq path (substring path 1)))))
  2863.       
  2864.       ;; Now substitute the expanded path back into the overall filename.
  2865.       (ange-ftp-replace-path-component n path))
  2866.       
  2867.       ;; non-ange-ftp path.  Just expand normally.
  2868.       (if (eq (string-to-char n) ?/)
  2869.       (ange-ftp-real-expand-file-name n)
  2870.     (ange-ftp-real-expand-file-name
  2871.      (ange-ftp-real-file-name-nondirectory n)
  2872.      (ange-ftp-real-file-name-directory n))))))
  2873.  
  2874. (defun ange-ftp-expand-file-name (name &optional default)
  2875.   "Documented as original."
  2876.   (ange-ftp-save-match-data
  2877.     (if (eq (string-to-char name) ?/)
  2878.     (while (cond ((string-match "[^:]+//" name) ;don't upset Apollo users
  2879.               (setq name (substring name (1- (match-end 0)))))
  2880.              ((string-match "/~" name)
  2881.               (setq name (substring name (1- (match-end 0))))))))
  2882.     (cond ((eq (string-to-char name) ?~)
  2883.        (ange-ftp-real-expand-file-name name))
  2884.       ((eq (string-to-char name) ?/)
  2885.        (ange-ftp-canonize-filename name))
  2886.       ((zerop (length name))
  2887.        (ange-ftp-canonize-filename (or default default-directory)))
  2888.       ((ange-ftp-canonize-filename
  2889.         (concat (file-name-as-directory (or default default-directory))
  2890.             name))))))
  2891.  
  2892. ;;;; ------------------------------------------------------------
  2893. ;;;; Redefinitions of standard GNU Emacs functions.
  2894. ;;;; ------------------------------------------------------------
  2895.  
  2896. (defvar ange-ftp-file-name-as-directory-alist nil
  2897.   "Association list of \( TYPE \. FUNC \) pairs, where
  2898. FUNC converts a filename to a directory name for the operating
  2899. system TYPE.")
  2900.  
  2901. (defun ange-ftp-file-name-as-directory (name)
  2902.   "Documented as original."
  2903.   (let ((parsed (ange-ftp-ftp-path name)))
  2904.     (if parsed
  2905.     (if (string-equal (nth 2 parsed) "")
  2906.         name
  2907.       (funcall (or (cdr (assq
  2908.                  (ange-ftp-host-type (car parsed))
  2909.                  ange-ftp-file-name-as-directory-alist))
  2910.                'ange-ftp-real-file-name-as-directory)
  2911.            name))
  2912.       (ange-ftp-real-file-name-as-directory name))))
  2913.      
  2914. (defun ange-ftp-file-name-directory (name)
  2915.   "Documented as original."
  2916.   (let ((parsed (ange-ftp-ftp-path name)))
  2917.     (if parsed
  2918.     (let ((path (nth 2 parsed)))
  2919.       (if (ange-ftp-save-match-data
  2920.         (string-match "^~[^/]*$" path))
  2921.           name
  2922.         (ange-ftp-replace-path-component
  2923.          name
  2924.          (ange-ftp-real-file-name-directory path))))
  2925.       (ange-ftp-real-file-name-directory name))))
  2926.  
  2927. (defun ange-ftp-file-name-nondirectory (name)
  2928.   "Documented as original."
  2929.   (let ((parsed (ange-ftp-ftp-path name)))
  2930.     (if parsed
  2931.     (let ((path (nth 2 parsed)))
  2932.       (if (ange-ftp-save-match-data
  2933.         (string-match "^~[^/]*$" path))
  2934.           ""
  2935.         (ange-ftp-real-file-name-nondirectory path)))
  2936.       (ange-ftp-real-file-name-nondirectory name))))
  2937.  
  2938. (defun ange-ftp-directory-file-name (dir)
  2939.   "Documented as original."
  2940.   (let ((parsed (ange-ftp-ftp-path dir)))
  2941.     (if parsed
  2942.     (ange-ftp-replace-path-component
  2943.        dir
  2944.        (ange-ftp-real-directory-file-name (nth 2 parsed)))
  2945.       (ange-ftp-real-directory-file-name dir))))
  2946.  
  2947. (defun ange-ftp-binary-file (file)
  2948.   "Returns whether the given FILE is to be considered as a binary file for
  2949. ftp transfers."
  2950.   (ange-ftp-save-match-data
  2951.     (string-match ange-ftp-binary-file-name-regexp file)))
  2952.  
  2953. (defun ange-ftp-write-region (start end filename &optional append visit)
  2954.   "Documented as original."
  2955.   (interactive "r\nFWrite region to file: ")
  2956.   (setq filename (expand-file-name filename))
  2957.   (let ((parsed (ange-ftp-ftp-path filename)))
  2958.     (if parsed
  2959.     (let* ((host (nth 0 parsed))
  2960.            (user (nth 1 parsed))
  2961.            (path (ange-ftp-quote-string (nth 2 parsed)))
  2962.            (temp (ange-ftp-make-tmp-name host))
  2963.            (binary (ange-ftp-binary-file filename))
  2964.            (cmd (if append 'append 'put))
  2965.            (abbr (ange-ftp-abbreviate-filename filename)))
  2966.       (unwind-protect
  2967.           (progn
  2968.         (let ((executing-macro t)
  2969.               (filename (buffer-file-name))
  2970.               (mod-p (buffer-modified-p)))
  2971.           (unwind-protect
  2972.               (ange-ftp-real-write-region start end temp nil visit)
  2973.             ;; cleanup forms
  2974.             (setq buffer-file-name filename)
  2975.             (if (fboundp 'compute-buffer-file-truename)
  2976.             (compute-buffer-file-truename))
  2977.             (set-buffer-modified-p mod-p)))
  2978.         (if binary
  2979.             (ange-ftp-set-binary-mode host user))
  2980.  
  2981.         ;; tell the process filter what size the transfer will be.
  2982.         (let ((attr (file-attributes temp)))
  2983.           (if attr
  2984.               (ange-ftp-set-xfer-size host user (nth 7 attr))))
  2985.  
  2986.         ;; put or append the file.
  2987.         (let ((result (ange-ftp-send-cmd host user
  2988.                          (list cmd temp path)
  2989.                          (format "Writing %s" abbr))))
  2990.           (or (car result)
  2991.               (signal 'ftp-error
  2992.                   (list
  2993.                    "Opening output file"
  2994.                    (format "FTP Error: \"%s\"" (cdr result))
  2995.                    filename)))))
  2996.         (ange-ftp-del-tmp-name temp)
  2997.         (if binary 
  2998.         (ange-ftp-set-ascii-mode host user)))
  2999.       (if (eq visit t)
  3000.           (progn
  3001.         (ange-ftp-set-buffer-mode)
  3002.         (setq buffer-file-name filename)
  3003.         (if (fboundp 'compute-buffer-file-truename)
  3004.             (compute-buffer-file-truename))
  3005.         (set-buffer-modified-p nil)))
  3006.       (ange-ftp-message "Wrote %s" abbr)
  3007.       (ange-ftp-add-file-entry filename))
  3008.       (ange-ftp-real-write-region start end filename append visit))))
  3009.  
  3010. (defun ange-ftp-insert-file-contents (filename &optional visit beg end replace)
  3011.   "Documented as original."
  3012.   (barf-if-buffer-read-only)
  3013.   (setq filename (expand-file-name filename))
  3014.   (let ((parsed (ange-ftp-ftp-path filename)))
  3015.     (if parsed
  3016.     (progn
  3017.       (if visit
  3018.           (progn
  3019.         (setq buffer-file-name filename)
  3020.         (if (fboundp 'compute-buffer-file-truename)
  3021.             (compute-buffer-file-truename))))
  3022.       (if (or (file-exists-p filename)
  3023.           (progn
  3024.             (setq ange-ftp-ls-cache-file nil)
  3025.             (ange-ftp-del-hash-entry (file-name-directory filename)
  3026.                          ange-ftp-files-hashtable)
  3027.             (file-exists-p filename)))
  3028.           (let* ((host (nth 0 parsed))
  3029.              (user (nth 1 parsed))
  3030.              (path (ange-ftp-quote-string (nth 2 parsed)))
  3031.              (temp (ange-ftp-make-tmp-name host))
  3032.              (binary (ange-ftp-binary-file filename))
  3033.              (abbr (ange-ftp-abbreviate-filename filename))
  3034.              size)
  3035.         (unwind-protect
  3036.             (progn
  3037.               (if binary
  3038.               (ange-ftp-set-binary-mode host user))
  3039.               (let ((result (ange-ftp-send-cmd host user
  3040.                           (list 'get path temp)
  3041.                           (format "Retrieving %s" abbr))))
  3042.             (or (car result)
  3043.                 (signal 'ftp-error
  3044.                     (list
  3045.                      "Opening input file"
  3046.                      (format "FTP Error: \"%s\"" (cdr result))
  3047.                      filename))))
  3048.               (if (or (ange-ftp-real-file-readable-p temp)
  3049.                   (sleep-for ange-ftp-retry-time)
  3050.                   ;; Wait for file to hopefully appear.
  3051.                   (ange-ftp-real-file-readable-p temp))
  3052.               (setq
  3053.                size
  3054.                (nth 1 (progn
  3055.                     (if replace ; kludge...
  3056.                     (delete-region (point-min)
  3057.                                (point-max)))
  3058.                     (ange-ftp-real-insert-file-contents
  3059.                      temp visit beg end nil))))
  3060.             (signal 'ftp-error
  3061.                 (list
  3062.                  "Opening input file:"
  3063.                  (format
  3064.                   "FTP Error: %s not arrived or readable"
  3065.                   filename)))))
  3066.           (if binary
  3067.               (ange-ftp-set-ascii-mode host user))
  3068.           (ange-ftp-del-tmp-name temp))
  3069.         (if visit
  3070.             (progn
  3071.               (setq buffer-file-name filename)
  3072.               (if (fboundp 'compute-buffer-file-truename)
  3073.               (compute-buffer-file-truename))))
  3074.         (list filename size))
  3075.         (signal 'file-error
  3076.             (list 
  3077.              "Opening input file"
  3078.              filename))))
  3079.       (ange-ftp-real-insert-file-contents filename visit beg end replace))))
  3080.  
  3081. (defun ange-ftp-revert-buffer (arg noconfirm)
  3082.   "Revert this buffer from a remote file using ftp."
  3083.   (let ((opoint (point)))
  3084.     (cond ((null buffer-file-name)
  3085.        (error "Buffer does not seem to be associated with any file"))
  3086.       ((or noconfirm
  3087.            (yes-or-no-p (format "Revert buffer from file %s? "
  3088.                     buffer-file-name)))
  3089.        (let ((buffer-read-only nil))
  3090.          ;; Set buffer-file-name to nil
  3091.          ;; so that we don't try to lock the file.
  3092.          (let ((buffer-file-name nil))
  3093.            (unlock-buffer)
  3094.            (erase-buffer))
  3095.          (insert-file-contents buffer-file-name t))
  3096.        (goto-char (min opoint (point-max)))
  3097.        (after-find-file nil)
  3098.        t))))
  3099.  
  3100. (defun ange-ftp-expand-symlink (file dir)
  3101.   (if (file-name-absolute-p file)
  3102.       (ange-ftp-replace-path-component dir file)
  3103.     (expand-file-name file dir)))
  3104.  
  3105. (defun ange-ftp-file-symlink-p (file)
  3106.   "Documented as original."
  3107.   ;; call ange-ftp-expand-file-name rather than the normal
  3108.   ;; expand-file-name to stop loops when using a package that
  3109.   ;; redefines both file-symlink-p and expand-file-name.
  3110.   (setq file (ange-ftp-expand-file-name file))
  3111.   (if (ange-ftp-ftp-path file)
  3112.       (let ((file-ent
  3113.          (ange-ftp-get-hash-entry
  3114.           (ange-ftp-get-file-part file)
  3115.           (ange-ftp-get-files (file-name-directory file)))))
  3116.     (if (stringp file-ent)
  3117.         (if (file-name-absolute-p file-ent)
  3118.         (ange-ftp-replace-path-component
  3119.               (file-name-directory file) file-ent)
  3120.           file-ent)))
  3121.     (ange-ftp-real-file-symlink-p file)))
  3122.  
  3123. (defun ange-ftp-file-exists-p (path)
  3124.   "Documented as original."
  3125.   (setq path (expand-file-name path))
  3126.   (if (ange-ftp-ftp-path path)
  3127.       (if (ange-ftp-file-entry-p path)
  3128.       (let ((file-ent (ange-ftp-get-file-entry path)))
  3129.         (if (stringp file-ent)
  3130.         (file-exists-p
  3131.          (ange-ftp-expand-symlink file-ent
  3132.                       (file-name-directory
  3133.                        (directory-file-name path))))
  3134.           t)))
  3135.     (ange-ftp-real-file-exists-p path)))
  3136.  
  3137. (defun ange-ftp-file-directory-p (path)
  3138.   "Documented as original."
  3139.   (setq path (expand-file-name path))
  3140.   (if (ange-ftp-ftp-path path)
  3141.       ;; We do a file-name-as-directory on path here because some
  3142.       ;; machines (VMS) use a .DIR to indicate the filename associated
  3143.       ;; with a directory. This needs to be canonicalized.
  3144.       (let ((file-ent (ange-ftp-get-file-entry
  3145.                (ange-ftp-file-name-as-directory path))))
  3146.     (if (stringp file-ent)
  3147.         (file-directory-p
  3148.          (ange-ftp-expand-symlink file-ent
  3149.                       (file-name-directory
  3150.                        (directory-file-name path))))
  3151.       file-ent))
  3152.     (ange-ftp-real-file-directory-p path)))
  3153.  
  3154. (defun ange-ftp-directory-files (directory &optional full match
  3155.                        &rest v19-args)
  3156.   "Documented as original."
  3157.   (setq directory (expand-file-name directory))
  3158.   (if (ange-ftp-ftp-path directory)
  3159.       (progn
  3160.     (ange-ftp-barf-if-not-directory directory)
  3161.     (let ((tail (ange-ftp-hash-table-keys
  3162.              (ange-ftp-get-files directory)))
  3163.           files f)
  3164.       (setq directory (file-name-as-directory directory))
  3165.       (ange-ftp-save-match-data
  3166.         (while tail
  3167.           (setq f (car tail)
  3168.             tail (cdr tail))
  3169.           (if (or (not match) (string-match match f))
  3170.           (setq files
  3171.             (cons (if full (concat directory f) f) files)))))
  3172.       (nreverse files)))
  3173.     (apply 'ange-ftp-real-directory-files directory full match v19-args)))
  3174.  
  3175. (defun ange-ftp-file-attributes (file)
  3176.   "Documented as original."
  3177.   (setq file (expand-file-name file))
  3178.   (let ((parsed (ange-ftp-ftp-path file)))
  3179.     (if parsed
  3180.     (let ((part (ange-ftp-get-file-part file))
  3181.           (files (ange-ftp-get-files (file-name-directory file))))
  3182.       (if (ange-ftp-hash-entry-exists-p part files)
  3183.           (let ((host (nth 0 parsed))
  3184.             (user (nth 1 parsed))
  3185.             (path (nth 2 parsed))
  3186.             (dirp (ange-ftp-get-hash-entry part files)))
  3187.         (list (if (and (stringp dirp) (file-name-absolute-p dirp))
  3188.               (ange-ftp-expand-symlink dirp
  3189.                            (file-name-directory file))
  3190.             dirp)        ;0 file type
  3191.               -1        ;1 link count
  3192.               -1        ;2 uid
  3193.               -1        ;3 gid
  3194.               '(0 0)        ;4 atime
  3195.               '(0 0)        ;5 mtime
  3196.               '(0 0)        ;6 ctime
  3197.               -1        ;7 size
  3198.               (concat (if (stringp dirp) "l" (if dirp "d" "-"))
  3199.                   "?????????") ;8 mode
  3200.               nil        ;9 gid weird
  3201.               ;; Hack to give remote files a unique "inode number".
  3202.               ;; It's actually the sum of the characters in its name.
  3203.               (apply '+ (nconc (mapcar 'identity host)
  3204.                        (mapcar 'identity user)
  3205.                        (mapcar 'identity
  3206.                            (directory-file-name path))))
  3207.               -1        ;11 device number [v19 only]
  3208.               ))))
  3209.       (ange-ftp-real-file-attributes file))))
  3210.  
  3211. (defun ange-ftp-file-writable-p (file)
  3212.   "Documented as original."
  3213.   (setq file (expand-file-name file))
  3214.   (if (ange-ftp-ftp-path file)
  3215.       (or (file-exists-p file)        ;guess here for speed
  3216.       (file-directory-p (file-name-directory file)))
  3217.     (ange-ftp-real-file-writable-p file)))
  3218.  
  3219. (defun ange-ftp-file-readable-p (file)
  3220.   "Documented as original."
  3221.   (setq file (expand-file-name file))
  3222.   (if (ange-ftp-ftp-path file)
  3223.       (file-exists-p file)
  3224.     (ange-ftp-real-file-readable-p file)))
  3225.  
  3226. (defun ange-ftp-delete-file (file)
  3227.   "Documented as original."
  3228.   (interactive "fDelete file: ")
  3229.   (setq file (expand-file-name file))
  3230.   (let ((parsed (ange-ftp-ftp-path file)))
  3231.     (if parsed
  3232.     (let* ((host (nth 0 parsed))
  3233.            (user (nth 1 parsed))
  3234.            (path (ange-ftp-quote-string (nth 2 parsed)))
  3235.            (abbr (ange-ftp-abbreviate-filename file))
  3236.            (result (ange-ftp-send-cmd host user
  3237.                       (list 'delete path)
  3238.                       (format "Deleting %s" abbr))))
  3239.       (or (car result)
  3240.           (signal 'ftp-error
  3241.               (list
  3242.                "Removing old name"
  3243.                (format "FTP Error: \"%s\"" (cdr result))
  3244.                file)))
  3245.       (ange-ftp-delete-file-entry file))
  3246.       (ange-ftp-real-delete-file file))))
  3247.  
  3248. (defun ange-ftp-verify-visited-file-modtime (buf)
  3249.   "Documented as original."
  3250.   (let ((name (buffer-file-name buf)))
  3251.     (if (and (stringp name) (ange-ftp-ftp-path name))
  3252.     t
  3253.       (ange-ftp-real-verify-visited-file-modtime buf))))
  3254.  
  3255. (defun ange-ftp-backup-buffer ()
  3256.   "Documented as original."
  3257.   (let (parsed)
  3258.     (if (and
  3259.      (listp ange-ftp-make-backup-files)
  3260.      (stringp buffer-file-name)
  3261.      (setq parsed (ange-ftp-ftp-path buffer-file-name))
  3262.      (or
  3263.       (null ange-ftp-make-backup-files)
  3264.       (not
  3265.        (memq
  3266.         (ange-ftp-host-type
  3267.          (car parsed))
  3268.         ange-ftp-make-backup-files))))
  3269.     nil
  3270.       (ange-ftp-real-backup-buffer))))
  3271.  
  3272. ;;;; ------------------------------------------------------------
  3273. ;;;; File copying support... totally re-written 6/24/92.
  3274. ;;;; ------------------------------------------------------------
  3275.  
  3276. (defun ange-ftp-barf-or-query-if-file-exists (absname querystring interactive)
  3277.   (if (file-exists-p absname)
  3278.       (if (not interactive)
  3279.       (signal 'file-already-exists (list absname))
  3280.     (if (not (yes-or-no-p (format "File %s already exists; %s anyway? "
  3281.                       absname querystring)))
  3282.         (signal 'file-already-exists (list absname))))))
  3283.  
  3284. ;; async local copy commented out for now since I don't seem to get
  3285. ;; the process sentinel called for some processes.
  3286. ;;
  3287. ;; (defun ange-ftp-copy-file-locally (filename newname ok-if-already-exists
  3288. ;;                         keep-date cont)
  3289. ;;   "Kludge to copy a local file and call a continuation when the copy
  3290. ;; finishes."
  3291. ;;   ;; check to see if we can overwrite
  3292. ;;   (if (or (not ok-if-already-exists)
  3293. ;;       (numberp ok-if-already-exists))
  3294. ;;       (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3295. ;;                          (numberp ok-if-already-exists)))
  3296. ;;   (let ((proc (start-process " *copy*"
  3297. ;;                  (generate-new-buffer "*copy*")
  3298. ;;                  "cp"
  3299. ;;                  filename
  3300. ;;                  newname))
  3301. ;;     res)
  3302. ;;     (set-process-sentinel proc (function ange-ftp-copy-file-locally-sentinel))
  3303. ;;     (process-kill-without-query proc)
  3304. ;;     (save-excursion
  3305. ;;       (set-buffer (process-buffer proc))
  3306. ;;       (make-variable-buffer-local 'copy-cont)
  3307. ;;       (setq copy-cont cont))))
  3308. ;; 
  3309. ;; (defun ange-ftp-copy-file-locally-sentinel (proc status)
  3310. ;;   (save-excursion
  3311. ;;     (set-buffer (process-buffer proc))
  3312. ;;     (let ((cont copy-cont)
  3313. ;;       (result (buffer-string)))
  3314. ;;       (unwind-protect
  3315. ;;       (if (and (string-equal status "finished\n")
  3316. ;;            (zerop (length result)))
  3317. ;;           (ange-ftp-call-cont cont t nil)
  3318. ;;         (ange-ftp-call-cont cont
  3319. ;;                 nil
  3320. ;;                 (if (zerop (length result))
  3321. ;;                     (substring status 0 -1)
  3322. ;;                   (substring result 0 -1))))
  3323. ;;     (kill-buffer (current-buffer))))))
  3324.  
  3325. ;; this is the extended version of ange-ftp-copy-file-internal that works
  3326. ;; asyncronously if asked nicely.
  3327. (defun ange-ftp-copy-file-internal (filename newname ok-if-already-exists
  3328.                          keep-date &optional msg cont nowait)
  3329.   (setq filename (expand-file-name filename)
  3330.     newname (expand-file-name newname))
  3331.  
  3332.   ;; canonicalize newname if a directory.
  3333.   (if (file-directory-p newname)
  3334.       (setq newname (expand-file-name (file-name-nondirectory filename) newname)))
  3335.  
  3336.   (let ((f-parsed (ange-ftp-ftp-path filename))
  3337.     (t-parsed (ange-ftp-ftp-path newname)))
  3338.  
  3339.     ;; local file to local file copy?
  3340.     (if (and (not f-parsed) (not t-parsed))
  3341.     (progn
  3342.       (ange-ftp-real-copy-file filename newname ok-if-already-exists
  3343.                    keep-date)
  3344.       (if cont
  3345.           (ange-ftp-call-cont cont t "Copied locally")))
  3346.       ;; one or both files are remote.
  3347.       (let* ((f-host (and f-parsed (nth 0 f-parsed)))
  3348.          (f-user (and f-parsed (nth 1 f-parsed)))
  3349.          (f-path (and f-parsed (ange-ftp-quote-string (nth 2 f-parsed))))
  3350.          (f-abbr (ange-ftp-abbreviate-filename filename))
  3351.          (t-host (and t-parsed (nth 0 t-parsed)))
  3352.          (t-user (and t-parsed (nth 1 t-parsed)))
  3353.          (t-path (and t-parsed (ange-ftp-quote-string (nth 2 t-parsed))))
  3354.          (t-abbr (ange-ftp-abbreviate-filename newname filename))
  3355.          (binary (or (ange-ftp-binary-file filename)
  3356.              (ange-ftp-binary-file newname)))
  3357.          temp1
  3358.          temp2)
  3359.  
  3360.     ;; check to see if we can overwrite
  3361.     (if (or (not ok-if-already-exists)
  3362.         (numberp ok-if-already-exists))
  3363.         (ange-ftp-barf-or-query-if-file-exists newname "copy to it" 
  3364.                            (numberp ok-if-already-exists)))
  3365.  
  3366.     ;; do the copying.
  3367.     (if f-parsed
  3368.         
  3369.         ;; filename was remote.
  3370.         (progn
  3371.           (if (or (ange-ftp-use-gateway-p f-host)
  3372.               t-parsed)
  3373.           ;; have to use intermediate file if we are getting via
  3374.           ;; gateway machine or we are doing a remote to remote copy.
  3375.           (setq temp1 (ange-ftp-make-tmp-name f-host)))
  3376.           
  3377.           (if binary
  3378.           (ange-ftp-set-binary-mode f-host f-user))
  3379.  
  3380.           (ange-ftp-send-cmd
  3381.            f-host
  3382.            f-user
  3383.            (list 'get f-path (or temp1 newname))
  3384.            (or msg
  3385.            (if (and temp1 t-parsed)
  3386.                (format "Getting %s" f-abbr)
  3387.              (format "Copying %s to %s" f-abbr t-abbr)))
  3388.            (list (function ange-ftp-cf1)
  3389.              filename newname binary msg
  3390.              f-parsed f-host f-user f-path f-abbr
  3391.              t-parsed t-host t-user t-path t-abbr
  3392.              temp1 temp2 cont nowait)
  3393.            nowait))
  3394.  
  3395.       ;; filename wasn't remote.  newname must be remote.  call the
  3396.       ;; function which does the remainder of the copying work.
  3397.       (ange-ftp-cf1 t nil
  3398.             filename newname binary msg
  3399.             f-parsed f-host f-user f-path f-abbr
  3400.             t-parsed t-host t-user t-path t-abbr
  3401.             nil nil cont nowait))))))
  3402.  
  3403. ;; next part of copying routine.
  3404. (defun ange-ftp-cf1 (result line
  3405.                 filename newname binary msg
  3406.                 f-parsed f-host f-user f-path f-abbr
  3407.                 t-parsed t-host t-user t-path t-abbr
  3408.                 temp1 temp2 cont nowait)
  3409.   (if line
  3410.       ;; filename must have been remote, and we must have just done a GET.
  3411.       (unwind-protect
  3412.       (or result
  3413.           ;; GET failed for some reason.  Clean up and get out.
  3414.           (progn
  3415.         (and temp1 (ange-ftp-del-tmp-name temp1))
  3416.         (or cont
  3417.             (signal 'ftp-error (list "Opening input file"
  3418.                          (format "FTP Error: \"%s\"" line)
  3419.                          filename)))))
  3420.     ;; cleanup
  3421.     (if binary
  3422.         (ange-ftp-set-ascii-mode f-host f-user))))
  3423.  
  3424.   (if result
  3425.       ;; We now have to copy either temp1 or filename to newname.
  3426.       (if t-parsed
  3427.       
  3428.       ;; newname was remote.
  3429.       (progn
  3430.         (if (ange-ftp-use-gateway-p t-host)
  3431.         (setq temp2 (ange-ftp-make-tmp-name t-host)))
  3432.         
  3433.         ;; make sure data is moved into the right place for the
  3434.         ;; outgoing transfer.  gateway temporary files complicate
  3435.         ;; things nicely.
  3436.         (if temp1
  3437.         (if temp2
  3438.             (if (string-equal temp1 temp2)
  3439.             (setq temp1 nil)
  3440.               (ange-ftp-real-copy-file temp1 temp2 t))
  3441.           (setq temp2 temp1 temp1 nil))
  3442.           (if temp2
  3443.           (ange-ftp-real-copy-file filename temp2 t)))
  3444.         
  3445.         (if binary
  3446.         (ange-ftp-set-binary-mode t-host t-user))
  3447.  
  3448.         ;; tell the process filter what size the file is.
  3449.         (let ((attr (file-attributes (or temp2 filename))))
  3450.           (if attr
  3451.           (ange-ftp-set-xfer-size t-host t-user (nth 7 attr))))
  3452.  
  3453.         (ange-ftp-send-cmd
  3454.          t-host
  3455.          t-user
  3456.          (list 'put (or temp2 filename) t-path)
  3457.          (or msg
  3458.          (if (and temp2 f-parsed)
  3459.              (format "Putting %s" newname)
  3460.            (format "Copying %s to %s" f-abbr t-abbr)))
  3461.          (list (function ange-ftp-cf2)
  3462.            newname t-host t-user binary temp1 temp2 cont)
  3463.          nowait))
  3464.     
  3465.     ;; newname wasn't remote.
  3466.     (ange-ftp-cf2 t nil newname t-host t-user binary temp1 temp2 cont))
  3467.  
  3468.     ;; first copy failed, tell caller
  3469.     (ange-ftp-call-cont cont result line)))
  3470.  
  3471. ;; last part of copying routine.
  3472. (defun ange-ftp-cf2 (result line newname t-host t-user binary temp1 temp2 cont)
  3473.   (unwind-protect
  3474.       (if line
  3475.       ;; result from doing a local to remote copy.
  3476.       (unwind-protect
  3477.           (progn
  3478.         (or result
  3479.             (or cont
  3480.             (signal 'ftp-error
  3481.                 (list "Opening output file"
  3482.                       (format "FTP Error: \"%s\"" line)
  3483.                       newname))))
  3484.         
  3485.         (ange-ftp-add-file-entry newname))
  3486.         
  3487.         ;; cleanup.
  3488.         (if binary
  3489.         (ange-ftp-set-ascii-mode t-host t-user)))
  3490.           
  3491.     ;; newname was local.
  3492.     (if temp1
  3493.         (ange-ftp-real-copy-file temp1 newname t)))
  3494.       
  3495.     ;; clean up
  3496.     (and temp1 (ange-ftp-del-tmp-name temp1))
  3497.     (and temp2 (ange-ftp-del-tmp-name temp2))
  3498.     (ange-ftp-call-cont cont result line)))
  3499.  
  3500. (defun ange-ftp-copy-file (filename newname &optional ok-if-already-exists
  3501.                     keep-date)
  3502.   "Documented as original."
  3503.   (interactive "fCopy file: \nFCopy %s to file: \np")
  3504.   (ange-ftp-copy-file-internal filename
  3505.                    newname
  3506.                    ok-if-already-exists
  3507.                    keep-date
  3508.                    nil
  3509.                    nil
  3510.                    (interactive-p)))
  3511.  
  3512. ;;;; ------------------------------------------------------------
  3513. ;;;; File renaming support.
  3514. ;;;; ------------------------------------------------------------
  3515.  
  3516. (defun ange-ftp-rename-remote-to-remote (filename newname f-parsed t-parsed
  3517.                           binary)
  3518.   "Rename remote file FILE to remote file NEWNAME."
  3519.   (let ((f-host (nth 0 f-parsed))
  3520.     (f-user (nth 1 f-parsed))
  3521.     (t-host (nth 0 t-parsed))
  3522.     (t-user (nth 1 t-parsed)))
  3523.     (if (and (string-equal f-host t-host)
  3524.          (string-equal f-user t-user))
  3525.     (let* ((f-path (ange-ftp-quote-string (nth 2 f-parsed)))
  3526.            (t-path (ange-ftp-quote-string (nth 2 t-parsed)))
  3527.            (cmd (list 'rename f-path t-path))
  3528.            (fabbr (ange-ftp-abbreviate-filename filename))
  3529.            (nabbr (ange-ftp-abbreviate-filename newname filename))
  3530.            (result (ange-ftp-send-cmd f-host f-user cmd
  3531.                       (format "Renaming %s to %s"
  3532.                           fabbr
  3533.                           nabbr))))
  3534.       (or (car result)
  3535.           (signal 'ftp-error
  3536.               (list
  3537.                "Renaming"
  3538.                (format "FTP Error: \"%s\"" (cdr result))
  3539.                filename
  3540.                newname)))
  3541.       (ange-ftp-add-file-entry newname)
  3542.       (ange-ftp-delete-file-entry filename))
  3543.       (ange-ftp-copy-file-internal filename newname t nil)
  3544.       (delete-file filename))))
  3545.  
  3546. (defun ange-ftp-rename-local-to-remote (filename newname)
  3547.   "Rename local FILE to remote file NEWNAME."
  3548.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3549.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3550.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3551.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3552.     (let (ange-ftp-process-verbose)
  3553.       (delete-file filename))))
  3554.  
  3555. (defun ange-ftp-rename-remote-to-local (filename newname)
  3556.   "Rename remote file FILE to local file NEWNAME."
  3557.   (let* ((fabbr (ange-ftp-abbreviate-filename filename))
  3558.      (nabbr (ange-ftp-abbreviate-filename newname filename))
  3559.      (msg (format "Renaming %s to %s" fabbr nabbr)))
  3560.     (ange-ftp-copy-file-internal filename newname t nil msg)
  3561.     (let (ange-ftp-process-verbose)
  3562.       (delete-file filename))))
  3563.  
  3564. (defun ange-ftp-rename-file (filename newname &optional ok-if-already-exists)
  3565.   "Documented as original."
  3566.   (interactive "fRename file: \nFRename %s to file: \np")
  3567.   (setq filename (expand-file-name filename))
  3568.   (setq newname (expand-file-name newname))
  3569.   (let* ((f-parsed (ange-ftp-ftp-path filename))
  3570.      (t-parsed (ange-ftp-ftp-path newname))
  3571.      (binary (if (or f-parsed t-parsed) (ange-ftp-binary-file filename))))
  3572.     (if (and (or f-parsed t-parsed)
  3573.          (or (not ok-if-already-exists)
  3574.          (numberp ok-if-already-exists)))
  3575.     (ange-ftp-barf-or-query-if-file-exists
  3576.      newname
  3577.      "rename to it"
  3578.      (numberp ok-if-already-exists)))
  3579.     (if f-parsed
  3580.     (if t-parsed
  3581.         (ange-ftp-rename-remote-to-remote filename newname f-parsed
  3582.                           t-parsed binary)
  3583.       (ange-ftp-rename-remote-to-local filename newname))
  3584.       (if t-parsed
  3585.       (ange-ftp-rename-local-to-remote filename newname)
  3586.     (ange-ftp-real-rename-file filename newname ok-if-already-exists)))))
  3587.  
  3588. ;;;; ------------------------------------------------------------
  3589. ;;;; Classic Dired support.
  3590. ;;;; ------------------------------------------------------------
  3591.  
  3592. (defvar ange-ftp-dired-host-type nil
  3593.   "The host type associated with a dired buffer. (buffer local)")
  3594. (make-variable-buffer-local 'ange-ftp-dired-host-type)
  3595.  
  3596. (defun ange-ftp-dired-readin (dirname buffer)
  3597.   "Documented as original."
  3598.   (let ((file (ange-ftp-abbreviate-filename dirname))
  3599.     (parsed (ange-ftp-ftp-path dirname)))
  3600.     (save-excursion
  3601.       (ange-ftp-message "Reading directory %s..." file)
  3602.       (set-buffer buffer)
  3603.       (let ((buffer-read-only nil))
  3604.     (widen)
  3605.     (erase-buffer)
  3606.     (setq dirname (expand-file-name dirname))
  3607.     (if parsed
  3608.         (let ((host-type (ange-ftp-host-type (car parsed))))
  3609.           (setq ange-ftp-dired-host-type host-type)
  3610.           (insert (ange-ftp-ls dirname dired-listing-switches t)))
  3611.       (if (ange-ftp-real-file-directory-p dirname)
  3612.           (call-process "ls" nil buffer nil
  3613.                 dired-listing-switches dirname)
  3614.         (let ((default-directory
  3615.             (ange-ftp-real-file-name-directory dirname)))
  3616.           (call-process
  3617.            shell-file-name nil buffer nil
  3618.            "-c" (concat
  3619.              "ls " dired-listing-switches " "
  3620.              (ange-ftp-real-file-name-nondirectory dirname))))))
  3621.     (goto-char (point-min))
  3622.     (while (not (eobp))
  3623.       (insert "  ")
  3624.       (forward-line 1))
  3625.     (goto-char (point-min))))
  3626.     (ange-ftp-message "Reading directory %s...done" file)))
  3627.  
  3628. (defun ange-ftp-dired-revert (&optional arg noconfirm)
  3629.   "Documented as original."
  3630.   (if (and dired-directory
  3631.        (ange-ftp-ftp-path (expand-file-name dired-directory)))
  3632.       (setq ange-ftp-ls-cache-file nil))
  3633.   (ange-ftp-real-dired-revert arg noconfirm))
  3634.  
  3635. ;;;; ------------------------------------------------------------
  3636. ;;;; Tree Dired support (ange & Sebastian Kremer)
  3637. ;;;; ------------------------------------------------------------
  3638.  
  3639. (defvar ange-ftp-dired-re-exe-alist nil
  3640.   "Association list of regexps \(strings\) which match file lines of
  3641.  executable files.")
  3642.  
  3643. (defvar ange-ftp-dired-re-dir-alist nil
  3644.   "Association list of regexps \(strings\) which match file lines of
  3645.  subdirectories.")
  3646.  
  3647. (defvar ange-ftp-dired-insert-headerline-alist nil
  3648.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3649. the function to be used by dired to insert the headerline of
  3650. the dired buffer.")
  3651.  
  3652. (defvar ange-ftp-dired-move-to-filename-alist nil
  3653.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3654. the function to be used by dired to move to the beginning of a
  3655. filename.")
  3656.  
  3657. (defvar ange-ftp-dired-move-to-end-of-filename-alist nil
  3658.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3659. the function to be used by dired to move to the end of a
  3660. filename.")
  3661.  
  3662. (defvar ange-ftp-dired-get-filename-alist nil
  3663.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3664. the function to be used by dired to get a filename from the
  3665. current line.")
  3666.  
  3667. (defvar ange-ftp-dired-between-files-alist nil
  3668.   "Association list of \(TYPE \. FUNC \) pairs, where FUNC is
  3669. the function to be used by dired to determine when the point
  3670. is on a line between files.")
  3671.  
  3672. (defvar ange-ftp-dired-ls-trim-alist nil
  3673.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3674. a function which trims extraneous lines from a directory listing.")
  3675.  
  3676. (defvar ange-ftp-dired-clean-directory-alist nil
  3677.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3678. a function which cleans out old versions of files in the OS TYPE.")
  3679.  
  3680. (defvar ange-ftp-dired-flag-backup-files-alist nil
  3681.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC is
  3682. a functions which flags the backup files for deletion in the OS TYPE.")
  3683.  
  3684. (defvar ange-ftp-dired-backup-diff-alist nil
  3685.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC diffs
  3686. a file with its backup. The backup file is determined according to
  3687. the OS TYPE.")
  3688.  
  3689. ;; Could use dired-before-readin-hook here, instead of overloading
  3690. ;; dired-readin. However, if people change this hook after ange-ftp
  3691. ;; is loaded, they'll break things.
  3692. ;; Also, why overload dired-readin rather than dired-mode?
  3693. ;; Because I don't want to muck up virtual dired (see dired-x.el).
  3694.  
  3695. (defun ange-ftp-tree-dired-readin (dirname buffer)
  3696.   "Documented as original."
  3697.   (let ((parsed (ange-ftp-ftp-path dirname)))
  3698.     (if parsed
  3699.     (save-excursion
  3700.       (set-buffer buffer)
  3701.       (setq ange-ftp-dired-host-type
  3702.         (ange-ftp-host-type (car parsed)))
  3703.       (and ange-ftp-dl-dir-regexp
  3704.            (eq ange-ftp-dired-host-type 'unix)
  3705.            (string-match ange-ftp-dl-dir-regexp dirname)
  3706.            (setq ange-ftp-dired-host-type 'unix:dl))
  3707.       (let ((eentry (assq ange-ftp-dired-host-type
  3708.                   ange-ftp-dired-re-exe-alist))
  3709.         (dentry (assq ange-ftp-dired-host-type
  3710.                   ange-ftp-dired-re-dir-alist)))
  3711.         (if eentry
  3712.         (set (make-local-variable 'dired-re-exe) (cdr eentry)))
  3713.         (if dentry
  3714.         (set (make-local-variable 'dired-re-dir) (cdr dentry)))
  3715.         ;; No switches are sent to dumb hosts, so don't confuse dired.
  3716.         ;; I hope that dired doesn't get excited if it doesn't see the l
  3717.         ;; switch. If it does, then maybe fake things by setting this to
  3718.         ;; "-Al".
  3719.         (if (memq ange-ftp-dired-host-type ange-ftp-dumb-host-types)
  3720.         (setq dired-actual-switches "-Al"))))))
  3721.   (ange-ftp-real-dired-readin dirname buffer))
  3722.  
  3723. (defun ange-ftp-dired-insert-headerline (dir)
  3724.   "Documented as original."
  3725.   (funcall (or (and ange-ftp-dired-host-type
  3726.             (cdr (assq ange-ftp-dired-host-type
  3727.                    ange-ftp-dired-insert-headerline-alist)))
  3728.            'ange-ftp-real-dired-insert-headerline)
  3729.        dir))
  3730.  
  3731. (defun ange-ftp-dired-move-to-filename (&optional raise-error eol)
  3732.   "Documented as original."
  3733.   (funcall (or (and ange-ftp-dired-host-type
  3734.             (cdr (assq ange-ftp-dired-host-type
  3735.                    ange-ftp-dired-move-to-filename-alist)))
  3736.            'ange-ftp-real-dired-move-to-filename)
  3737.        raise-error eol))
  3738.  
  3739. (defun ange-ftp-dired-move-to-end-of-filename (&optional no-error)
  3740.   "Documented as original."
  3741.   (funcall (or (and ange-ftp-dired-host-type
  3742.             (cdr (assq ange-ftp-dired-host-type
  3743.                    ange-ftp-dired-move-to-end-of-filename-alist)))
  3744.            'ange-ftp-real-dired-move-to-end-of-filename)
  3745.        no-error))
  3746.  
  3747. (defun ange-ftp-dired-get-filename (&optional localp no-error-if-not-filep)
  3748.   "Documented as original."
  3749.   (funcall (or (and ange-ftp-dired-host-type
  3750.             (cdr (assq ange-ftp-dired-host-type
  3751.                    ange-ftp-dired-get-filename-alist)))
  3752.            'ange-ftp-real-dired-get-filename)
  3753.        localp no-error-if-not-filep))
  3754.  
  3755. (defun ange-ftp-dired-between-files ()
  3756.   "Documented as original."
  3757.   (funcall (or (and ange-ftp-dired-host-type
  3758.             (cdr (assq ange-ftp-dired-host-type
  3759.                    ange-ftp-dired-between-files-alist)))
  3760.            'ange-ftp-real-dired-between-files)))
  3761.  
  3762. (defvar ange-ftp-bob-version-alist nil
  3763.   "Association list of pairs \( TYPE \. FUNC \), where FUNC is
  3764. a function to be used to bob the version number off of a filename
  3765. in OS TYPE.")
  3766.  
  3767. (defun ange-ftp-dired-find-file ()
  3768.   "Documented as original."
  3769.   (interactive)
  3770.   (find-file (funcall (or (and ange-ftp-dired-host-type
  3771.                    (cdr (assq ange-ftp-dired-host-type
  3772.                       ange-ftp-bob-version-alist)))
  3773.               'identity)
  3774.               (dired-get-filename))))
  3775.  
  3776. ;; Need the following functions for making filenames of compressed
  3777. ;; files, because some OS's (unlike UNIX) do not allow a filename to
  3778. ;; have two extensions.
  3779.  
  3780. (defvar ange-ftp-dired-compress-make-compressed-filename-alist nil
  3781.   "Association list of \( TYPE \. FUNC \) pairs, where FUNC converts a
  3782. filename to the filename of the associated compressed file.")
  3783.  
  3784. ;;; this overwrites dired's `dired-compress-make-compressed-filename'
  3785. (defun ange-ftp-dired-compress-make-compressed-filename (name &optional reverse)
  3786.   "Converts a filename to the filename of the associated compressed
  3787. file. With an optional reverse argument, the reverse conversion is done.
  3788.  
  3789. Modified to work with gzip (GNU zip) files."
  3790.   (let ((parsed (ange-ftp-ftp-path name))
  3791.     conversion-func)
  3792.     (if (and parsed
  3793.          (setq conversion-func
  3794.            (cdr (assq (ange-ftp-host-type (car parsed))
  3795.                   ange-ftp-dired-compress-make-compressed-filename-alist))))
  3796.     (funcall conversion-func name reverse)
  3797.       (if reverse
  3798.  
  3799.           ;; uncompress...
  3800.           ;; return `nil' if no match found -- better than nothing
  3801.           (let (case-fold-search ; case-sensitive search
  3802.                 (string
  3803.                  (concat "\\.\\(g?z\\|" (regexp-quote dired-gzip-file-extension)
  3804.                          "$\\|Z\\)$")))
  3805.             
  3806.             (and (string-match string name)
  3807.                  (substring name 0 (match-beginning 0))))
  3808.  
  3809.         ;; add appropriate extension
  3810.         ;; note: it could be that `gz' is not the proper extension for gzip
  3811.         (concat name
  3812.                 (if dired-use-gzip-instead-of-compress
  3813.                     dired-gzip-file-extension ".Z"))))))
  3814.  
  3815. (defun ange-ftp-dired-clean-directory (keep)
  3816.   "Documented as original."
  3817.   (interactive "P")
  3818.   (funcall (or (and ange-ftp-dired-host-type
  3819.             (cdr (assq ange-ftp-dired-host-type
  3820.                    ange-ftp-dired-clean-directory-alist)))
  3821.            'ange-ftp-real-dired-clean-directory)
  3822.        keep))
  3823.  
  3824. (defun ange-ftp-dired-backup-diff (&optional switches)
  3825.   "Documented as original."
  3826.   (interactive (list (if (fboundp 'diff-read-switches)
  3827.              (diff-read-switches "Diff with switches: "))))
  3828.   (funcall (or (and ange-ftp-dired-host-type
  3829.             (cdr (assq ange-ftp-dired-host-type
  3830.                    ange-ftp-dired-backup-diff-alist)))
  3831.            'ange-ftp-real-dired-backup-diff)
  3832.        switches))
  3833.  
  3834.  
  3835. (defun ange-ftp-dired-fixup-subdirs (start file)
  3836.   "Turn each subdir name into a valid ange-ftp filename."
  3837.  
  3838.   ;; We haven't indented the listing yet.
  3839.   ;; Must be careful about filelines ending in a colon: exclude spaces!
  3840.   (let ((subdir-regexp "^\\([^ \n\r]+\\)\\(:\\)[\n\r]"))
  3841.     (save-restriction
  3842.       (save-excursion
  3843.     (narrow-to-region start (point))
  3844.     (goto-char start)
  3845.     (while (re-search-forward subdir-regexp nil t)
  3846.       (goto-char (match-beginning 1))
  3847.       (let ((name (buffer-substring (point)
  3848.                     (match-end 1))))
  3849.         (delete-region (point) (match-end 1))
  3850.         (insert (ange-ftp-replace-path-component
  3851.              file
  3852.              name))))))))
  3853.  
  3854. (defun ange-ftp-dired-ls (file switches &optional wildcard full-directory-p)
  3855.   "Documented as original."
  3856.   (let ((parsed (ange-ftp-ftp-path file)))
  3857.     (if parsed
  3858.     (let* ((pt (point))
  3859.            (path (nth 2 parsed))
  3860.            (host-type (ange-ftp-host-type (car parsed)))
  3861.            (dumb (memq host-type ange-ftp-dumb-host-types))
  3862.            trim-func case-fold-search)
  3863.       ;; Make sure that case-fold-search is nil
  3864.       ;; so that we can look at the switches.
  3865.       (if wildcard
  3866.           (if (not (memq host-type '(unix dumb-unix)))
  3867.           (insert (ange-ftp-ls file switches nil))
  3868.         ;; Prevent ls from inserting subdirs, as the subdir header
  3869.         ;; line format would be wrong (it would have no "/user@host:"
  3870.         ;; prefix)
  3871.         (insert (ange-ftp-ls file (concat switches "d") nil))
  3872.  
  3873.         ;; Quoting the path part of the file name seems to be a good
  3874.         ;; idea (using dired.el's shell-quote function), but ftpd
  3875.         ;; always globs ls args before passing them to /bin/ls or even
  3876.         ;; doing the ls formatting itself.  --> So wildcard characters
  3877.         ;; in FILE lose.  Sigh...
  3878.  
  3879.         ;; When using wildcards, some ftpd's put the whole directory
  3880.         ;; name in front of each filename.  Walk down the listing
  3881.         ;; generated and remove this stuff.
  3882.         (let ((dir (ange-ftp-real-file-name-directory path)))
  3883.           (if dir
  3884.               (let ((dirq (regexp-quote dir)))
  3885.             (save-restriction
  3886.               (save-excursion
  3887.                 (narrow-to-region pt (point))
  3888.                 (goto-char pt)
  3889.                 (while (not (eobp))
  3890.                   (if (dired-move-to-filename)
  3891.                   (if (re-search-forward dirq nil t)
  3892.                       (replace-match "")))
  3893.                   (forward-line 1))))))))
  3894.  
  3895.                ;;;;;;;;;;;;;;;;;;;;;;;;;;
  3896.                ;; Big issue here Andy! ;;
  3897.            ;;;;;;;;;;;;;;;;;;;;;;;;;;
  3898.            ;; In tree dired V5.245 Sebastian has used the following
  3899.            ;; trick to resolve symbolic links to directories. This causes
  3900.            ;; havoc with ange-ftp, because ange-ftp expands dots, with
  3901.            ;; expand-file-name before it sends them. This means that this
  3902.            ;; trick currently fails for remote SysV machines. But worse,
  3903.            ;; /vms:/DEV:/FOO/. expands to /vms:/DEV:/FOO, which converts
  3904.            ;; to DEV:FOO and not DEV:[FOO]. i.e it is only in UNIX that
  3905.            ;; we can play fast and loose with the difference between
  3906.            ;; directory names and their associated filenames.
  3907.            ;; My temporary fix is to knock Sebastian's dot off.
  3908.            ;; Maybe things can be made real clever in
  3909.            ;; the future, so that Sebastian can have his way with remote
  3910.            ;; SysV machines.
  3911.            ;; Sebastian in dired-readin-insert says:
  3912.  
  3913.               ;; On SysV derived system, symbolic links to
  3914.           ;; directories are not resolved, while on BSD
  3915.           ;; derived it suffices to let DIRNAME end in slash.
  3916.           ;; We always let it end in "/." since it does no
  3917.           ;; harm on BSD and makes Dired work on such links on
  3918.           ;; SysV.
  3919.  
  3920.         (if (string-match "/\\.$" path)
  3921.         (setq
  3922.          file
  3923.          (ange-ftp-replace-path-component
  3924.           file (substring path 0 -1))))
  3925.         (if (string-match "R" switches)
  3926.         (progn
  3927.           (insert (ange-ftp-ls file switches nil))
  3928.           ;; fix up the subdirectory names in the recursive
  3929.           ;; listing.
  3930.           (ange-ftp-dired-fixup-subdirs pt file))
  3931.           (insert
  3932.            (ange-ftp-ls file
  3933.                 switches
  3934.                 (and (or dumb (string-match "[aA]" switches))
  3935.                  full-directory-p))))
  3936.         (if (and (null full-directory-p)
  3937.              (setq trim-func
  3938.                (cdr (assq host-type
  3939.                       ange-ftp-dired-ls-trim-alist))))
  3940.         ;; If full-directory-p and wild-card are null, then only one
  3941.         ;; line per file must be inserted.
  3942.         ;; Some OS's (like VMS) insert other crap. Clean it out.
  3943.         (save-restriction
  3944.           (narrow-to-region pt (point))
  3945.           (funcall trim-func)))))
  3946.       (ange-ftp-real-dired-ls file switches wildcard full-directory-p))))
  3947.  
  3948. (defvar ange-ftp-remote-shell-file-name
  3949.   (if (memq system-type '(hpux usg-unix-v)) ; hope that's right
  3950.       "remsh"
  3951.     "rsh")
  3952.   "Remote shell used by ange-ftp.")
  3953.  
  3954. (defun ange-ftp-dired-run-shell-command (command &optional in-background)
  3955.   "Documented as original."
  3956.   (let* ((parsed (ange-ftp-ftp-path default-directory))
  3957.      (host (nth 0 parsed))
  3958.      (user (nth 1 parsed))
  3959.      (path (nth 2 parsed)))
  3960.     (if (not parsed)
  3961.     (ange-ftp-real-dired-run-shell-command command in-background)
  3962.       (if (> (length path) 0)        ; else it's $HOME
  3963.       (setq command (concat "cd " path "; " command)))
  3964.       (setq command
  3965.         (format  "%s %s \"%s\""    ; remsh -l USER does not work well
  3966.                     ; on a hp-ux machine I tried
  3967.              ange-ftp-remote-shell-file-name host command))
  3968.       (ange-ftp-message "Remote command '%s' ..." command)
  3969.       ;; Cannot call ange-ftp-real-dired-run-shell-command here as it
  3970.       ;; would prepend "cd default-directory" --- which bombs because
  3971.       ;; default-directory is in ange-ftp syntax for remote path names.
  3972.       (if in-background
  3973.       (comint::background command)
  3974.     (shell-command command)))))
  3975.  
  3976. (defun ange-ftp-make-directory (dir &optional parents)
  3977.   "Documented as original."
  3978.   (interactive (list (let ((current-prefix-arg current-prefix-arg))
  3979.                (read-directory-name "Create directory: "))
  3980.              current-prefix-arg))
  3981.   (if (file-exists-p dir)
  3982.       (error "Cannot make directory %s: file already exists" dir)
  3983.     (let ((parsed (ange-ftp-ftp-path dir)))
  3984.       (if parsed
  3985.       (let* ((host (nth 0 parsed))
  3986.          (user (nth 1 parsed))
  3987.          ;; Some ftp's on unix machines (at least on Suns)
  3988.          ;; insist that mkdir take a filename, and not a
  3989.          ;; directory-name name as an arg. Argh!! This is a bug.
  3990.          ;; Non-unix machines will probably always insist
  3991.          ;; that mkdir takes a directory-name as an arg
  3992.          ;; (as the ftp man page says it should).
  3993.          (path (ange-ftp-quote-string
  3994.             (if (eq (ange-ftp-host-type host) 'unix)
  3995.                 (ange-ftp-real-directory-file-name (nth 2 parsed))
  3996.               (ange-ftp-real-file-name-as-directory
  3997.                (nth 2 parsed)))))
  3998.          (abbr (ange-ftp-abbreviate-filename dir))
  3999.          (result (ange-ftp-send-cmd host user
  4000.                         (list 'mkdir path)
  4001.                         (format "Making directory %s"
  4002.                             abbr))))
  4003.         (or (car result)
  4004.         (ange-ftp-error host user
  4005.                 (format "Could not make directory %s: %s"
  4006.                     dir
  4007.                     (cdr result))))
  4008.         (ange-ftp-add-file-entry dir t))
  4009.     (ange-ftp-real-make-directory dir parents)))))
  4010.  
  4011. (defun ange-ftp-remove-directory (dir)
  4012.   "Documented as original."
  4013.   (interactive 
  4014.    (list (expand-file-name (read-file-name "Remove directory: "
  4015.                        nil nil 'confirm)))) 
  4016.   (if (file-directory-p dir)
  4017.       (let ((parsed (ange-ftp-ftp-path dir)))
  4018.     (if parsed
  4019.         (let* ((host (nth 0 parsed))
  4020.            (user (nth 1 parsed))
  4021.            ;; Some ftp's on unix machines (at least on Suns)
  4022.            ;; insist that rmdir take a filename, and not a
  4023.            ;; directory-name name as an arg. Argh!! This is a bug.
  4024.            ;; Non-unix machines will probably always insist
  4025.            ;; that rmdir takes a directory-name as an arg
  4026.            ;; (as the ftp man page says it should).
  4027.            (path (ange-ftp-quote-string
  4028.               (if (eq (ange-ftp-host-type host) 'unix)
  4029.                   (ange-ftp-real-directory-file-name
  4030.                    (nth 2 parsed))
  4031.                 (ange-ftp-real-file-name-as-directory
  4032.                  (nth 2 parsed)))))
  4033.           (abbr (ange-ftp-abbreviate-filename dir))
  4034.           (result (ange-ftp-send-cmd host user
  4035.                          (list 'rmdir path)
  4036.                          (format "Removing directory %s"
  4037.                              abbr))))
  4038.           (or (car result)
  4039.           (ange-ftp-error host user
  4040.                   (format "Could not remove directory %s: %s"
  4041.                       dir
  4042.                       (cdr result))))
  4043.           (ange-ftp-delete-file-entry dir t))
  4044.       (ange-ftp-real-remove-directory dir)))
  4045.     (error "Not a directory: %s" dir)))
  4046.  
  4047. ;; XEmacs change: This function isn't in the FSF version.  Maybe
  4048. ;; because there is no such function as diff-read-args.  I can't find
  4049. ;; where there ever _has_ been such a function.  If you want this
  4050. ;; functionality, write diff-read-args and uncomment this.
  4051.  
  4052. ;;(defun ange-ftp-diff (fn1 fn2 &optional switches)
  4053. ;;  "Documented as original."
  4054. ;;  (interactive (diff-read-args "Diff: " "Diff %s with: "
  4055. ;;                   "Diff with switches: "))
  4056. ;;  (or (and (stringp fn1)
  4057. ;;       (stringp fn2))
  4058. ;;      (error  "diff: arguments must be strings: %s %s" fn1 fn2))
  4059. ;;  (or switches
  4060. ;;      (setq switches (if (stringp diff-switches)
  4061. ;;             diff-switches
  4062. ;;               (if (listp diff-switches)
  4063. ;;               (mapconcat 'identity diff-switches " ")
  4064. ;;             ""))))
  4065. ;;  (let* ((fn1 (expand-file-name fn1))
  4066. ;;     (fn2 (expand-file-name fn2))
  4067. ;;     (pa1 (ange-ftp-ftp-path fn1))
  4068. ;;     (pa2 (ange-ftp-ftp-path fn2)))
  4069. ;;    (if (or pa1 pa2)
  4070. ;;    (let* ((tmp1 (and pa1 (ange-ftp-make-tmp-name (car pa1))))
  4071. ;;           (tmp2 (and pa2 (ange-ftp-make-tmp-name (car pa2))))
  4072. ;;           (bin1 (and pa1 (ange-ftp-binary-file fn1)))
  4073. ;;           (bin2 (and pa2 (ange-ftp-binary-file fn2)))
  4074. ;;           (dir1 (file-directory-p fn1))
  4075. ;;           (dir2 (file-directory-p fn2))
  4076. ;;           (old-dir default-directory)
  4077. ;;           (default-directory "/tmp")) ;fool FTP-smart compile.el
  4078. ;;      (unwind-protect
  4079. ;;          (progn
  4080. ;;        (if (and dir1 dir2)
  4081. ;;            (error "can't compare remote directories"))
  4082. ;;        (if dir1
  4083. ;;            (setq fn1 (expand-file-name (file-name-nondirectory fn2)
  4084. ;;                        fn1)
  4085. ;;              pa1 (ange-ftp-ftp-path fn1)
  4086. ;;              bin1 (ange-ftp-binary-file fn1)))
  4087. ;;        (if dir2
  4088. ;;            (setq fn2 (expand-file-name (file-name-nondirectory fn1)
  4089. ;;                        fn2)
  4090. ;;              pa2 (ange-ftp-ftp-path fn2)
  4091. ;;              bin2 (ange-ftp-binary-file fn2)))
  4092. ;;        (and pa1 (ange-ftp-copy-file-internal fn1 tmp1 t nil
  4093. ;;                   (format "Getting %s" fn1)))
  4094. ;;        (and pa2 (ange-ftp-copy-file-internal fn2 tmp2 t nil
  4095. ;;                   (format "Getting %s" fn2)))
  4096. ;;        (and ange-ftp-process-verbose
  4097. ;;             (ange-ftp-message "doing diff..."))
  4098. ;;        (sit-for 0)
  4099. ;;        (ange-ftp-real-diff (or tmp1 fn1) (or tmp2 fn2) switches)
  4100. ;;        (cond ((boundp 'compilation-process)
  4101. ;;               (while (and compilation-process
  4102. ;;                   (eq (process-status compilation-process)
  4103. ;;                       'run))
  4104. ;;             (accept-process-output compilation-process)))
  4105. ;;              ((boundp 'compilation-last-buffer)
  4106. ;;               (while (and compilation-last-buffer
  4107. ;;                   (buffer-name compilation-last-buffer)
  4108. ;;                   (get-buffer-process
  4109. ;;                    compilation-last-buffer)
  4110. ;;                   (eq (process-status
  4111. ;;                    (get-buffer-process
  4112. ;;                     compilation-last-buffer))
  4113. ;;                       'run))
  4114. ;;             (accept-process-output))))
  4115. ;;        (and ange-ftp-process-verbose
  4116. ;;             (ange-ftp-message "doing diff...done"))
  4117. ;;        (save-excursion
  4118. ;;          (set-buffer (get-buffer-create "*compilation*"))
  4119. ;;
  4120. ;;          ;; replace the default directory that we munged earlier.
  4121. ;;          (goto-char (point-min))
  4122. ;;          (if (search-forward (concat "cd " default-directory) nil t)
  4123. ;;              (replace-match (concat "cd " old-dir)))
  4124. ;;          (setq default-directory old-dir)
  4125. ;;
  4126. ;;          ;; massage the diff output, replacing the temporary file-
  4127. ;;          ;; names with their original names.
  4128. ;;          (if tmp1
  4129. ;;              (let ((q1 (shell-quote tmp1)))
  4130. ;;            (goto-char (point-min))
  4131. ;;            (while (search-forward q1 nil t)
  4132. ;;              (replace-match fn1))))
  4133. ;;          (if tmp2
  4134. ;;              (let ((q2 (shell-quote tmp2)))
  4135. ;;            (goto-char (point-min))
  4136. ;;            (while (search-forward q2 nil t)
  4137. ;;              (replace-match fn2))))))
  4138. ;;        (and tmp1 (ange-ftp-del-tmp-name tmp1))
  4139. ;;        (and tmp2 (ange-ftp-del-tmp-name tmp2))))
  4140. ;;      (ange-ftp-real-diff fn1 fn2 switches))))
  4141.         
  4142. (defun ange-ftp-dired-call-process (program discard &rest arguments)
  4143.   "Documented as original."
  4144.   ;; PROGRAM is always one of those below in the cond in dired.el.
  4145.   ;; The ARGUMENTS are (nearly) always files.
  4146.   (if (ange-ftp-ftp-path default-directory)
  4147.       ;; Can't use ange-ftp-dired-host-type here because the current
  4148.       ;; buffer is *dired-check-process output*
  4149.       (condition-case oops
  4150.       (cond ((equal "compress" program)
  4151.          (ange-ftp-call-compress arguments))
  4152.         ((equal "uncompress" program)
  4153.          (ange-ftp-call-uncompress arguments))
  4154.         ((equal "chmod" program)
  4155.          (ange-ftp-call-chmod arguments))
  4156.         ;; ((equal "chgrp" program))
  4157.         ;; ((equal dired-chown-program program))
  4158.         (t (error "Unknown remote command: %s" program)))
  4159.     (ftp-error (insert (format "%s: %s, %s\n"
  4160.                     (nth 1 oops)
  4161.                     (nth 2 oops)
  4162.                     (nth 3 oops))))
  4163.     (error (insert (format "%s\n" (nth 1 oops)))))
  4164.     (apply 'call-process program nil (not discard) nil arguments)))
  4165.  
  4166.  
  4167. (defun ange-ftp-call-compress (args)
  4168.   "Perform a compress command on a remote file.
  4169. Works by taking a copy of the file, compressing it and copying the file
  4170. back."
  4171.   (if (or (not (= (length args) 2))
  4172.       (not (string-equal "-f" (car args))))
  4173.       (error
  4174.        "ange-ftp-call-compress: missing -f flag and/or missing filename: %s"
  4175.        args))
  4176.   (let* ((file (nth 1 args))
  4177.      (parsed (ange-ftp-ftp-path file))
  4178.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  4179.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  4180.      (abbr (ange-ftp-abbreviate-filename file))
  4181.      (nfile (ange-ftp-dired-compress-make-compressed-filename file))
  4182.      (nabbr (ange-ftp-abbreviate-filename nfile))
  4183.      (msg1 (format "Getting %s" abbr))
  4184.      (msg2 (format "Putting %s" nabbr)))
  4185.     (unwind-protect
  4186.     (progn
  4187.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  4188.       (and ange-ftp-process-verbose
  4189.            (ange-ftp-message "Compressing %s..." abbr))
  4190.       (call-process-region (point)
  4191.                    (point)
  4192.                    shell-file-name
  4193.                    nil
  4194.                    t
  4195.                    nil
  4196.                    "-c"
  4197.                    (format "compress -f -c < %s > %s" tmp1 tmp2))
  4198.       (and ange-ftp-process-verbose
  4199.            (ange-ftp-message "Compressing %s...done" abbr))
  4200.       (if (zerop (buffer-size))
  4201.           (progn
  4202.         (let (ange-ftp-process-verbose)
  4203.           (delete-file file))
  4204.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  4205.       (ange-ftp-del-tmp-name tmp1)
  4206.       (ange-ftp-del-tmp-name tmp2))))
  4207.     
  4208. (defun ange-ftp-call-uncompress (args)
  4209.   "Perform an uncompress command on a remote file.
  4210. Works by taking a copy of the file, uncompressing it and copying the file
  4211. back."
  4212.   (if (not (= (length args) 1))
  4213.       (error "ange-ftp-call-uncompress: missing filename: %s" args))
  4214.   (let* ((file (car args))
  4215.      (parsed (ange-ftp-ftp-path file))
  4216.      (tmp1 (ange-ftp-make-tmp-name (car parsed)))
  4217.      (tmp2 (ange-ftp-make-tmp-name (car parsed)))
  4218.      (abbr (ange-ftp-abbreviate-filename file))
  4219.      (nfile (ange-ftp-dired-compress-make-compressed-filename file 'reverse))
  4220.      (nabbr (ange-ftp-abbreviate-filename nfile))
  4221.      (msg1 (format "Getting %s" abbr))
  4222.      (msg2 (format "Putting %s" nabbr))
  4223. ;;     ;; Cheap hack because of problems with binary file transfers from
  4224. ;;     ;; VMS hosts.
  4225. ;;     (gbinary (not (eq 'vms (ange-ftp-host-type (car parsed)))))
  4226.      )
  4227.     (unwind-protect
  4228.     (progn
  4229.       (ange-ftp-copy-file-internal file tmp1 t nil msg1)
  4230.       (and ange-ftp-process-verbose
  4231.            (ange-ftp-message "Uncompressing %s..." abbr))
  4232.       (call-process-region (point)
  4233.                    (point)
  4234.                    shell-file-name
  4235.                    nil
  4236.                    t
  4237.                    nil
  4238.                    "-c"
  4239.                    (format "uncompress -c < %s > %s" tmp1 tmp2))
  4240.       (and ange-ftp-process-verbose
  4241.            (ange-ftp-message "Uncompressing %s...done" abbr))
  4242.       (if (zerop (buffer-size))
  4243.           (progn
  4244.         (let (ange-ftp-process-verbose)
  4245.           (delete-file file))
  4246.         (ange-ftp-copy-file-internal tmp2 nfile t nil msg2))))
  4247.       (ange-ftp-del-tmp-name tmp1)
  4248.       (ange-ftp-del-tmp-name tmp2))))
  4249.  
  4250. (defun ange-ftp-call-chmod (args)
  4251.   (if (< (length args) 2)
  4252.       (error "ange-ftp-call-chmod: missing mode and/or filename: %s" args))
  4253.   (let ((mode (car args)))
  4254.     (mapcar
  4255.      (function
  4256.       (lambda (file)
  4257.     (setq file (expand-file-name file))
  4258.     (let ((parsed (ange-ftp-ftp-path file)))
  4259.       (if parsed
  4260.           (let* ((host (nth 0 parsed))
  4261.              (user (nth 1 parsed))
  4262.              (path (ange-ftp-quote-string (nth 2 parsed)))
  4263.              (abbr (ange-ftp-abbreviate-filename file))
  4264.              (result (ange-ftp-send-cmd host user
  4265.                         (list 'chmod mode path)
  4266.                         (format "doing chmod %s"
  4267.                             abbr))))
  4268.         (or (car result)
  4269.             (ange-ftp-error host user
  4270.                     (format "chmod: %s: \"%s\""
  4271.                         file 
  4272.                         (cdr result)))))))))
  4273.      (cdr args)))
  4274.   (setq ange-ftp-ls-cache-file nil))    ;stop confusing dired
  4275.  
  4276. ;; Need to abstract the way dired computes the names of compressed files.
  4277. ;; I feel badly about these two overloads.
  4278.  
  4279. (defun ange-ftp-dired-compress ()
  4280.   ;; Compress current file.  Return nil for success, offending filename else.
  4281.   (let* (buffer-read-only
  4282.      (from-file (dired-get-filename))
  4283.      (to-file (ange-ftp-dired-compress-make-compressed-filename from-file)))
  4284.     (cond ((save-excursion (beginning-of-line)
  4285.                (looking-at dired-re-sym))
  4286.        (dired-log (concat "Attempt to compress a symbolic link:\n"
  4287.                   from-file))
  4288.        (dired-make-relative from-file))
  4289.       ((dired-check-process (concat "Compressing " from-file)
  4290.                 "compress" "-f" from-file)
  4291.        ;; errors from the process are already logged by
  4292.        ;; dired-check-process
  4293.        (dired-make-relative from-file))
  4294.       (t
  4295.      (dired-update-file-line to-file)
  4296.      nil))))
  4297.  
  4298. (defun ange-ftp-dired-uncompress ()
  4299.   ;; Uncompress current file.  Return nil for success,
  4300.   ;; offending filename else.
  4301.   (let* (buffer-read-only
  4302.      (from-file (dired-get-filename))
  4303.      (to-file (ange-ftp-dired-compress-make-compressed-filename from-file 'reverse)))
  4304.     (if (dired-check-process (concat "Uncompressing " from-file)
  4305.                  "uncompress" from-file)
  4306.     (dired-make-relative from-file)
  4307.       (dired-update-file-line to-file)
  4308.       nil)))
  4309.  
  4310. (defun ange-ftp-dired-flag-backup-files (&optional unflag-p)
  4311.   "Documented as original."
  4312.   (interactive "P")
  4313.   (funcall (or (and ange-ftp-dired-host-type
  4314.             (cdr (assq ange-ftp-dired-host-type
  4315.                    ange-ftp-dired-flag-backup-files-alist)))
  4316.            'ange-ftp-real-dired-flag-backup-files)
  4317.        unflag-p))
  4318.  
  4319. ;;; ------------------------------------------------------------
  4320. ;;; Noddy support for async copy-file within dired.
  4321. ;;; ------------------------------------------------------------
  4322.  
  4323. (defun ange-ftp-dired-copy-file (from to ok-flag &optional cont nowait)
  4324.   "Documented as original."
  4325.   (dired-handle-overwrite to)
  4326.   (ange-ftp-copy-file-internal from to ok-flag dired-copy-preserve-time nil
  4327.                    cont nowait))
  4328.  
  4329. (defun ange-ftp-dired-do-create-files (op-symbol file-creator operation arg
  4330.                          &optional marker-char op1
  4331.                          how-to)
  4332.   "Documented as original."
  4333.   ;; we need to let ange-ftp-dired-create-files know that we indirectly
  4334.   ;; called it rather than somebody else.
  4335.   (let ((ange-ftp-dired-do-create-files t)) ; tell who caller is
  4336.     (ange-ftp-real-dired-do-create-files op-symbol file-creator operation
  4337.                      arg marker-char op1 how-to)))
  4338.  
  4339. (defun ange-ftp-dired-create-files (file-creator operation fn-list name-constructor
  4340.                          &optional marker-char)
  4341.   "Documented as original."
  4342.   (if (and (boundp 'ange-ftp-dired-do-create-files)
  4343.        ;; called from ange-ftp-dired-do-create-files?
  4344.        ange-ftp-dired-do-create-files
  4345.        ;; any files worth copying?
  4346.        fn-list
  4347.        ;; we only support async copy-file at the mo.
  4348.        (eq file-creator 'dired-copy-file)
  4349.        ;; it is only worth calling the alternative function for remote files
  4350.        ;; as we tie ourself in recursive knots otherwise.
  4351.        (or (ange-ftp-ftp-path (car fn-list))
  4352.            ;; we can only call the name constructor for dired-do-create-files
  4353.            ;; since the one for regexps starts prompting here, there and
  4354.            ;; everywhere.
  4355.            (ange-ftp-ftp-path (funcall name-constructor (car fn-list)))))
  4356.       ;; use the process-filter driven routine rather than the iterative one.
  4357.       (ange-ftp-dcf-1 file-creator
  4358.                  operation
  4359.                fn-list
  4360.                name-constructor
  4361.                (and (boundp 'target) target)    ;dynamically bound
  4362.                marker-char
  4363.                (current-buffer)
  4364.                nil    ;overwrite-query
  4365.                nil    ;overwrite-backup-query
  4366.                nil    ;failures
  4367.                nil    ;skipped
  4368.                0        ;success-count
  4369.                (length fn-list) ;total
  4370.                )
  4371.     ;; normal case... use the interative routine... much cheaper.
  4372.     (ange-ftp-real-dired-create-files file-creator operation fn-list
  4373.                       name-constructor marker-char)))
  4374.  
  4375. (defun ange-ftp-dcf-1 (file-creator operation fn-list name-constructor
  4376.                target marker-char buffer overwrite-query 
  4377.                overwrite-backup-query failures skipped
  4378.                success-count total)
  4379.   (let ((old-buf (current-buffer)))
  4380.     (unwind-protect
  4381.     (progn
  4382.       (set-buffer buffer)
  4383.       (if (null fn-list)
  4384.           (ange-ftp-dcf-3 failures operation total skipped
  4385.                   success-count buffer)
  4386.         
  4387.         (let* ((from (car fn-list))
  4388.            (to (funcall name-constructor from)))
  4389.           (if (equal to from)
  4390.           (progn
  4391.             (setq to nil)
  4392.             (dired-log "Cannot %s to same file: %s\n"
  4393.                    (downcase operation) from)))
  4394.           (if (not to)
  4395.           (ange-ftp-dcf-1 file-creator
  4396.                   operation
  4397.                   (cdr fn-list)
  4398.                   name-constructor
  4399.                   target
  4400.                   marker-char
  4401.                   buffer
  4402.                   overwrite-query
  4403.                   overwrite-backup-query
  4404.                   failures
  4405.                   (cons (dired-make-relative from) skipped)
  4406.                   success-count
  4407.                   total)
  4408.         (let* ((overwrite (file-exists-p to))
  4409.                (overwrite-confirmed    ; for dired-handle-overwrite
  4410.             (and overwrite
  4411.                  (let ((help-form '(format "\
  4412. Type SPC or `y' to overwrite file `%s',
  4413. DEL or `n' to skip to next,
  4414. ESC or `q' to not overwrite any of the remaining files,
  4415. `!' to overwrite all remaining files with no more questions." to)))
  4416.                    (dired-query 'overwrite-query
  4417.                         "Overwrite `%s'?" to))))
  4418.                ;; must determine if FROM is marked before file-creator
  4419.                ;; gets a chance to delete it (in case of a move).
  4420.                (actual-marker-char
  4421.             (cond  ((integerp marker-char) marker-char)
  4422.                    (marker-char (dired-file-marker from)) ; slow
  4423.                    (t nil))))
  4424.           (condition-case err
  4425.               (funcall file-creator from to overwrite-confirmed
  4426.                    (list (function ange-ftp-dcf-2)
  4427.                      nil    ;err
  4428.                      file-creator operation fn-list
  4429.                      name-constructor
  4430.                      target
  4431.                      marker-char actual-marker-char
  4432.                      buffer to from
  4433.                      overwrite
  4434.                      overwrite-confirmed
  4435.                      overwrite-query 
  4436.                      overwrite-backup-query
  4437.                      failures skipped success-count
  4438.                      total)
  4439.                    t)
  4440.             (file-error        ; FILE-CREATOR aborted
  4441.              (ange-ftp-dcf-2 nil ;result
  4442.                      nil ;line
  4443.                      err
  4444.                      file-creator operation fn-list
  4445.                      name-constructor
  4446.                      target
  4447.                      marker-char actual-marker-char
  4448.                      buffer to from
  4449.                      overwrite
  4450.                      overwrite-confirmed
  4451.                      overwrite-query 
  4452.                      overwrite-backup-query
  4453.                      failures skipped success-count
  4454.                      total))))))))
  4455.       (set-buffer old-buf))))
  4456.  
  4457. (defun ange-ftp-dcf-2 (result line err
  4458.                   file-creator operation fn-list
  4459.                   name-constructor
  4460.                   target
  4461.                   marker-char actual-marker-char
  4462.                   buffer to from
  4463.                   overwrite
  4464.                   overwrite-confirmed
  4465.                   overwrite-query 
  4466.                   overwrite-backup-query
  4467.                   failures skipped success-count
  4468.                   total)
  4469.   (let ((old-buf (current-buffer)))
  4470.     (unwind-protect
  4471.     (progn
  4472.       (set-buffer buffer)
  4473.       (if (or err (not result))
  4474.           (progn
  4475.         (setq failures (cons (dired-make-relative from) failures))
  4476.         (dired-log "%s `%s' to `%s' failed:\n%s\n"
  4477.                operation from to (or err line)))
  4478.         (if overwrite
  4479.         ;; If we get here, file-creator hasn't been aborted
  4480.         ;; and the old entry (if any) has to be deleted
  4481.         ;; before adding the new entry.
  4482.         (dired-remove-file to))
  4483.         (setq success-count (1+ success-count))
  4484.         (message "%s: %d of %d" operation success-count total)
  4485.         (dired-add-file to actual-marker-char))
  4486.       
  4487.       (ange-ftp-dcf-1 file-creator operation (cdr fn-list)
  4488.               name-constructor
  4489.               target
  4490.               marker-char
  4491.               buffer
  4492.               overwrite-query 
  4493.               overwrite-backup-query
  4494.               failures skipped success-count
  4495.               total))
  4496.       (set-buffer old-buf))))
  4497.  
  4498. (defun ange-ftp-dcf-3 (failures operation total skipped success-count
  4499.                 buffer)
  4500.   (let ((old-buf (current-buffer)))
  4501.     (unwind-protect
  4502.     (progn
  4503.       (set-buffer buffer)
  4504.       (cond
  4505.        (failures
  4506.         (dired-log-summary
  4507.          (message "%s failed for %d of %d file%s %s"
  4508.               operation (length failures) total
  4509.               (dired-plural-s total) failures)))
  4510.        (skipped
  4511.         (dired-log-summary
  4512.          (message "%s: %d of %d file%s skipped %s"
  4513.               operation (length skipped) total
  4514.               (dired-plural-s total) skipped)))
  4515.        (t
  4516.         (message "%s: %s file%s."
  4517.              operation success-count (dired-plural-s success-count))))
  4518.       (dired-move-to-filename))
  4519.       (set-buffer old-buf))))
  4520.  
  4521. ;;;; -----------------------------------------------
  4522. ;;;; Unix Descriptive Listing (dl) Support
  4523. ;;;; -----------------------------------------------
  4524.  
  4525. (defconst ange-ftp-dired-dl-re-dir
  4526.   "^. [^ /]+/[ \n]"
  4527.   "Regular expression to use to search for dl directories.")
  4528.  
  4529. (or (assq 'unix:dl ange-ftp-dired-re-dir-alist)
  4530.     (setq ange-ftp-dired-re-dir-alist
  4531.       (cons (cons 'unix:dl  ange-ftp-dired-dl-re-dir)
  4532.         ange-ftp-dired-re-dir-alist)))
  4533.  
  4534. (defun ange-ftp-dired-dl-move-to-filename (&optional raise-error eol)
  4535.   "In dired, move to the first character of the filename on this line."
  4536.   ;; This is the Unix dl version.
  4537.   (or eol (setq eol (progn (end-of-line) (point))))
  4538.   (let (case-fold-search)
  4539.     (beginning-of-line)
  4540.     (if (looking-at ". [^ ]+ +\\([0-9]+\\|-\\|=\\) ")
  4541.     (goto-char (+ (point) 2))
  4542.       (if raise-error
  4543.       (error "No file on this line")
  4544.     nil))))
  4545.  
  4546. (or (assq 'unix:dl ange-ftp-dired-move-to-filename-alist)
  4547.     (setq ange-ftp-dired-move-to-filename-alist
  4548.       (cons '(unix:dl . ange-ftp-dired-dl-move-to-filename)
  4549.         ange-ftp-dired-move-to-filename-alist)))
  4550.  
  4551. (defun ange-ftp-dired-dl-move-to-end-of-filename (&optional no-error eol)
  4552.   ;; Assumes point is at beginning of filename.
  4553.   ;; So, it should be called only after (dired-move-to-filename t).
  4554.   ;; On failure, signals an error or returns nil.
  4555.   ;; This is the Unix dl version.
  4556.   (let ((opoint (point))
  4557.     case-fold-search hidden)
  4558.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  4559.     (setq hidden (and selective-display
  4560.                (save-excursion
  4561.              (search-forward "\r" eol t))))
  4562.     (if hidden
  4563.     (if no-error
  4564.         nil
  4565.       (error
  4566.        (substitute-command-keys
  4567.         "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  4568.       (skip-chars-forward "^ /" eol)
  4569.       (if (eq opoint (point))
  4570.       (if no-error
  4571.           nil
  4572.         (error "No file on this line"))
  4573.     (point)))))
  4574.  
  4575. (or (assq 'unix:dl ange-ftp-dired-move-to-end-of-filename-alist)
  4576.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  4577.       (cons '(unix:dl . ange-ftp-dired-dl-move-to-end-of-filename)
  4578.         ange-ftp-dired-move-to-end-of-filename-alist)))
  4579.  
  4580. ;;;; ------------------------------------------------------------
  4581. ;;;; File name completion support.
  4582. ;;;; ------------------------------------------------------------
  4583.  
  4584. (defun ange-ftp-file-entry-active-p (sym)
  4585.   "If the file entry is a symlink, returns whether the file pointed to exists.
  4586. Note that DIR is dynamically bound."
  4587.   (let ((val (get sym 'val)))
  4588.     (or (not (stringp val))
  4589.     (file-exists-p (ange-ftp-expand-symlink val dir)))))
  4590.  
  4591. (defun ange-ftp-file-entry-not-ignored-p (sym)
  4592.   "If the file entry is not a directory (nor a symlink pointing to a directory)
  4593. returns whether the file (or file pointed to by the symlink) is ignored
  4594. by completion-ignored-extensions.
  4595. Note that DIR and COMPLETION-IGNORED-PATTERN are dynamically bound."
  4596.   (let ((val (get sym 'val))
  4597.     (symname (symbol-name sym)))
  4598.     (if (stringp val)
  4599.     (let ((file (ange-ftp-expand-symlink val dir)))
  4600.       (or (file-directory-p file)
  4601.           (and (file-exists-p file)
  4602.            (not (string-match completion-ignored-pattern
  4603.                       symname)))))
  4604.       (or val ; is a directory name
  4605.       (not (string-match completion-ignored-pattern symname))))))
  4606.  
  4607. (defun ange-ftp-file-name-all-completions (file dir)
  4608.   "Documented as original."
  4609.   (setq dir (expand-file-name dir))
  4610.   (if (ange-ftp-ftp-path dir)
  4611.       (progn
  4612.     (ange-ftp-barf-if-not-directory dir)
  4613.     (setq dir (ange-ftp-real-file-name-as-directory dir))
  4614.     (let* ((tbl (ange-ftp-get-files dir))
  4615.            (completions
  4616.         (all-completions file tbl
  4617.                  (function ange-ftp-file-entry-active-p))))
  4618.       
  4619.       ;; see whether each matching file is a directory or not...
  4620.       (mapcar
  4621.        (function
  4622.         (lambda (file)
  4623.           (let ((ent (ange-ftp-get-hash-entry file tbl)))
  4624.         (if (and ent
  4625.              (or (not (stringp ent))
  4626.                  (file-directory-p
  4627.                   (ange-ftp-expand-symlink ent dir))))
  4628.             (concat file "/")
  4629.           file))))
  4630.        completions)))
  4631.     
  4632.     (if (string-equal "/" dir)
  4633.     (nconc (all-completions file (ange-ftp-generate-root-prefixes))
  4634.            (ange-ftp-real-file-name-all-completions file dir))
  4635.       (ange-ftp-real-file-name-all-completions file dir))))
  4636.  
  4637. (defun ange-ftp-file-name-completion (file dir)
  4638.   "Documented as original."
  4639.   (setq dir (expand-file-name dir))
  4640.   (if (ange-ftp-ftp-path dir)
  4641.       (progn
  4642.     (ange-ftp-barf-if-not-directory dir)
  4643.     (if (equal file "")
  4644.         ""
  4645.       (setq dir (ange-ftp-real-file-name-as-directory dir))    ;real?
  4646.       (let* ((tbl (ange-ftp-get-files dir))
  4647.          (completion-ignored-pattern
  4648.           (mapconcat (function
  4649.                   (lambda (s) (if (stringp s)
  4650.                           (concat (regexp-quote s) "$")
  4651.                         "/"))) ; / never in filename
  4652.                  completion-ignored-extensions
  4653.                  "\\|")))
  4654.         (ange-ftp-save-match-data
  4655.           (or (ange-ftp-file-name-completion-1
  4656.            file tbl dir (function ange-ftp-file-entry-not-ignored-p))
  4657.           (ange-ftp-file-name-completion-1
  4658.            file tbl dir (function ange-ftp-file-entry-active-p)))))))
  4659.     (if (string-equal "/" dir)
  4660.     (try-completion
  4661.      file
  4662.      (nconc (ange-ftp-generate-root-prefixes)
  4663.         (mapcar 'list
  4664.             (ange-ftp-real-file-name-all-completions file "/"))))
  4665.       (ange-ftp-real-file-name-completion file dir))))
  4666.  
  4667.  
  4668. (defun ange-ftp-file-name-completion-1 (file tbl dir predicate)
  4669.   "Internal subroutine for ange-ftp-file-name-completion.  Do not call this."
  4670.   (let ((bestmatch (try-completion file tbl predicate)))
  4671.     (if bestmatch
  4672.     (if (eq bestmatch t)
  4673.         (if (file-directory-p (expand-file-name file dir))
  4674.         (concat file "/")
  4675.           t)
  4676.       (if (and (eq (try-completion bestmatch tbl predicate) t)
  4677.            (file-directory-p
  4678.             (expand-file-name bestmatch dir)))
  4679.           (concat bestmatch "/")
  4680.         bestmatch)))))
  4681.  
  4682. (defun ange-ftp-quote-filename (file)
  4683.   "Quote `$' as `$$' in FILE to get it past function `substitute-in-file-name.'"
  4684.   (let ((pos 0))
  4685.     (while (setq pos (string-match "\\$" file pos))
  4686.       (setq file (concat (substring file 0 pos)
  4687.              "$";; precede by escape character (also a $)
  4688.              (substring file pos))
  4689.         ;; add 2 instead 1 since another $ was inserted
  4690.         pos (+ 2 pos)))
  4691.     file))
  4692.  
  4693. ;; (defun ange-ftp-read-file-name-internal (string dir action)
  4694. ;;   "Documented as original."
  4695. ;;   (let (name realdir)
  4696. ;;     (if (eq action 'lambda)
  4697. ;;     (if (> (length string) 0)
  4698. ;;         (file-exists-p (substitute-in-file-name string)))
  4699. ;;       (if (zerop (length string))
  4700. ;;      (setq name string realdir dir)
  4701. ;;        (setq string (substitute-in-file-name string)
  4702. ;;           name (file-name-nondirectory string)
  4703. ;;           realdir (file-name-directory string))
  4704. ;;     (setq realdir (if realdir (expand-file-name realdir dir) dir)))
  4705. ;;      (if action
  4706. ;;       (file-name-all-completions name realdir)
  4707. ;;     (let ((specdir (file-name-directory string))
  4708. ;;           (val (file-name-completion name realdir)))
  4709. ;;       (if (and specdir (stringp val))
  4710. ;;           (ange-ftp-quote-filename (concat specdir val))
  4711. ;;         val))))))
  4712.  
  4713. ;; Put these lines uncommmented in your .emacs if you want C-r to refresh
  4714. ;; ange-ftp's cache whilst doing filename completion.
  4715. ;;
  4716. ;;(define-key minibuffer-local-completion-map "\C-r" 'ange-ftp-re-read-dir)
  4717. ;;(define-key minibuffer-local-must-match-map "\C-r" 'ange-ftp-re-read-dir)
  4718.  
  4719. (defun ange-ftp-re-read-dir (&optional dir)
  4720.   "Forces a re-read of the directory DIR.  If DIR is omitted then it defaults
  4721. to the directory part of the contents of the current buffer."
  4722.   (interactive)
  4723.   (if dir
  4724.       (setq dir (expand-file-name dir))
  4725.     (setq dir (file-name-directory (expand-file-name (buffer-string)))))
  4726.   (if (ange-ftp-ftp-path dir)
  4727.       (progn
  4728.     (setq ange-ftp-ls-cache-file nil)
  4729.     (ange-ftp-del-hash-entry dir ange-ftp-files-hashtable)
  4730.     (ange-ftp-get-files dir t))))
  4731.  
  4732. ;;;; ------------------------------------------------------------
  4733. ;;;; Bits and bobs to bolt ange-ftp into GNU Emacs.
  4734. ;;;; ------------------------------------------------------------
  4735.  
  4736. (defvar ange-ftp-overwrite-msg
  4737.   "Note: This function has been modified to work with ange-ftp.")
  4738.  
  4739. (defun ange-ftp-safe-documentation (fun)
  4740.   "A documentation function that isn't quite as fragile."
  4741.   (condition-case ()
  4742.       (documentation fun)
  4743.     (error nil)))
  4744.  
  4745. (defun ange-ftp-overwrite-fn (fun)
  4746.   "Replace FUN's function definition with ange-ftp-FUN's, saving the
  4747. original definition as ange-ftp-real-FUN.  The original documentation is
  4748. placed on the new definition suitably augmented."
  4749.   (let* ((name (symbol-name fun))
  4750.      (saved (intern (concat "ange-ftp-real-" name)))
  4751.      (new (intern (concat "ange-ftp-" name)))
  4752.      (nfun (symbol-function new))
  4753.      (exec-directory (if (or (equal (nth 3 command-line-args) "dump")
  4754.                  (equal (nth 4 command-line-args) "dump"))
  4755.                  "../etc/"
  4756.                exec-directory)))             
  4757.     
  4758.     ;; *** This is unnecessary for any ange-ftp function (I think):
  4759.     (while (symbolp nfun)
  4760.       (setq nfun (symbol-function nfun)))
  4761.     
  4762.     ;; Interpose the ange-ftp function between the function symbol and the
  4763.     ;; original definition of the function symbol AT TIME OF FIRST LOAD.
  4764.     ;; We must only redefine the symbol-function of FUN the very first
  4765.     ;; time, to avoid blowing away stuff that overloads FUN after this.
  4766.     
  4767.     ;; We direct the function symbol to the ange-ftp's function symbol
  4768.     ;; rather than function definition to allow reloading of this file or
  4769.     ;; redefining of the individual function (e.g., during debugging)
  4770.     ;; later after some other code has been loaded on top of our stuff.
  4771.     
  4772.     (or (fboundp saved)
  4773.     (progn
  4774.       (fset saved (symbol-function fun))
  4775.       (fset fun new)))
  4776.     
  4777.     ;; Rewrite the doc string on the new ange-ftp function.  This should
  4778.     ;; be done every time the file is loaded (or a function is redefined),
  4779.     ;; because the underlying overloaded function may have changed its doc
  4780.     ;; string.
  4781.     
  4782.     (let* ((doc-str (ange-ftp-safe-documentation saved))
  4783.        (ndoc-str (concat doc-str (and doc-str "\n")
  4784.                  ange-ftp-overwrite-msg)))
  4785.       
  4786.       (cond ((listp nfun)
  4787.          ;; Probe to test whether function is in preloaded read-only
  4788.          ;; memory, and if so make writable copy:
  4789.          (condition-case nil
  4790.          (setcar nfun (car nfun))
  4791.            (error
  4792.         (setq nfun (copy-sequence nfun)) ; shallow copy only
  4793.         (fset new nfun)))
  4794.          (let ((ndoc-cdr (nthcdr 2 nfun)))
  4795.            (if (stringp (car ndoc-cdr))
  4796.            ;; Replace the existing docstring.
  4797.            (setcar ndoc-cdr ndoc-str)
  4798.          ;; There is no docstring.  Insert the overwrite msg.
  4799.          (setcdr ndoc-cdr (cons (car ndoc-cdr) (cdr ndoc-cdr)))
  4800.          (setcar ndoc-cdr ange-ftp-overwrite-msg))))
  4801.         (t
  4802.          ;; it's an emacs19 compiled-code object
  4803.          (if (not (fboundp 'compiled-function-arglist))
  4804.          ;; the old way (typical emacs lack of abstraction)
  4805.          (let ((new-code (append nfun nil))) ; turn it into a list
  4806.            (if (nthcdr 4 new-code)
  4807.                (setcar (nthcdr 4 new-code) ndoc-str)
  4808.              (setcdr (nthcdr 3 new-code) (cons ndoc-str nil)))
  4809.            (fset new (apply 'make-byte-code new-code)))
  4810.            ;; the new way (marginally less random) for XEmacs 19.8+
  4811.            (fset new
  4812.              (apply 'make-byte-code
  4813.                 (compiled-function-arglist nfun)
  4814.                 (compiled-function-instructions nfun)
  4815.                 (compiled-function-constants nfun)
  4816.                 (compiled-function-stack-depth nfun)
  4817.                 ndoc-str
  4818.                 (if (commandp nfun)
  4819.                 (list (nth 1 (compiled-function-interactive
  4820.                           nfun)))
  4821.                   nil)))
  4822.            ))))))
  4823.  
  4824. (defun ange-ftp-overwrite-dired ()
  4825.   (if (not (fboundp 'dired-ls))        ;dired should have been loaded by now
  4826.       (ange-ftp-overwrite-fn 'dired-readin) ; classic dired
  4827.     (ange-ftp-overwrite-fn 'make-directory) ; tree dired and v19 stuff
  4828.     (ange-ftp-overwrite-fn 'remove-directory)
  4829.     ;; XEmacs - not anymore because ange-ftp-diff is hosed
  4830.     ;; (ange-ftp-overwrite-fn 'diff)
  4831.     (ange-ftp-overwrite-fn 'dired-run-shell-command)
  4832.     (ange-ftp-overwrite-fn 'dired-ls)
  4833.     (ange-ftp-overwrite-fn 'dired-call-process)
  4834.     ;; Can't use (fset 'ange-ftp-dired-readin 'ange-ftp-tree-dired-readin)
  4835.     ;; here because it confuses ange-ftp-overwrite-fn.
  4836.     (fset 'ange-ftp-dired-readin (symbol-function 'ange-ftp-tree-dired-readin))
  4837.     (ange-ftp-overwrite-fn 'dired-readin)
  4838.     (ange-ftp-overwrite-fn 'dired-insert-headerline)
  4839.     (ange-ftp-overwrite-fn 'dired-move-to-filename)
  4840.     (ange-ftp-overwrite-fn 'dired-move-to-end-of-filename)
  4841.     (ange-ftp-overwrite-fn 'dired-get-filename)
  4842.     (ange-ftp-overwrite-fn 'dired-between-files)
  4843.     (ange-ftp-overwrite-fn 'dired-clean-directory)
  4844.     (ange-ftp-overwrite-fn 'dired-flag-backup-files)
  4845.     (ange-ftp-overwrite-fn 'dired-backup-diff)
  4846.     (if (fboundp 'dired-do-create-files)
  4847.     ;; dired 6.0 or later.
  4848.     (progn
  4849.       (ange-ftp-overwrite-fn 'dired-copy-file)
  4850.       (ange-ftp-overwrite-fn 'dired-create-files)
  4851.       (ange-ftp-overwrite-fn 'dired-do-create-files)))
  4852.     (if (fboundp 'dired-compress-make-compressed-filename)
  4853.     ;; it's V5.255 or later
  4854.     (ange-ftp-overwrite-fn 'dired-compress-make-compressed-filename)
  4855.       ;; ange-ftp-overwrite-fn confuses dired-mark-map here.
  4856.       (fset 'ange-ftp-real-dired-compress (symbol-function 'dired-compress))
  4857.       (fset 'dired-compress 'ange-ftp-dired-compress)
  4858.       (fset 'ange-ftp-real-dired-uncompress (symbol-function 'dired-uncompress))
  4859.       (fset 'dired-uncompress 'ange-ftp-dired-uncompress)))
  4860.  
  4861.   (ange-ftp-overwrite-fn 'dired-find-file)
  4862.   (ange-ftp-overwrite-fn 'dired-revert))
  4863.  
  4864. ;; Attention!
  4865. ;; It would be nice if ange-ftp-add-hook was generalized to
  4866. ;; (defun ange-ftp-add-hook (hook-var hook-function &optional postpend),
  4867. ;; where the optional postpend variable stipulates that hook-function
  4868. ;; should be post-pended to the hook-var, rather than prepended.
  4869. ;; Then, maybe we should overwrite dired with
  4870. ;; (ange-ftp-add-hook 'dired-load-hook 'ange-ftp-overwrite-dired t).
  4871. ;; This is because dired-load-hook is commonly used to add the dired extras
  4872. ;; features (dired-x.el, dired-trns.el, dired-nstd.el, ...). Some of these
  4873. ;; extras features overwrite functions in dired.el with fancier versions.
  4874. ;; The "extras" overwrites would then clobber the ange-ftp overwrites.
  4875. ;; As long as the ange-ftp overwrites are carefully written to use
  4876. ;; ange-ftp-real-... when the directory is local, then doing the ange-ftp
  4877. ;; overwrites after the extras overwites should be OK.
  4878. ;; At the moment, I think that there aren't any conflicts between the extras
  4879. ;; overwrites, and the ange-ftp overwrites. This may not last though.
  4880.  
  4881. (defun ange-ftp-add-hook (hook-var hook-function)
  4882.   "Prepend hook-function to hook-var's value, if it is not already an element.
  4883. hook-var's value may be a single function or a list of functions."
  4884.   (if (boundp hook-var)
  4885.       (let ((value (symbol-value hook-var)))
  4886.         (if (and (listp value) (not (eq (car value) 'lambda)))
  4887.             (and (not (memq hook-function value))
  4888.                  (set hook-var
  4889.                       (if value (cons hook-function value) hook-function)))
  4890.           (and (not (eq hook-function value))
  4891.                (set hook-var
  4892.                     (list hook-function value)))))
  4893.     (set hook-var hook-function)))
  4894.  
  4895. ;; To load ange-ftp and not dired (leaving it to autoload), define
  4896. ;; dired-load-hook and make sure dired.el ends with:
  4897. ;;    (run-hooks 'dired-load-hook)
  4898. ;;
  4899. (if (and (boundp 'dired-load-hook)
  4900.      (not (featurep 'dired)))
  4901.     (ange-ftp-add-hook 'dired-load-hook 'ange-ftp-overwrite-dired)
  4902.   (require 'dired)
  4903.   (ange-ftp-overwrite-dired))
  4904.  
  4905. ;; In case v19 or emacs-19.el already loaded:
  4906. ;; (Can't use fboundp to check if emacs-19.el is
  4907. ;; loaded, because these functions are probably
  4908. ;; bound to autoloads.)
  4909.  
  4910. (if (and (fboundp 'make-directory)
  4911.      (not (and (listp (symbol-function 'make-directory))
  4912.            (eq (car (symbol-function 'make-directory)) 'autoload))))
  4913.     (ange-ftp-overwrite-fn 'make-directory))
  4914. (if (and (fboundp 'remove-directory)
  4915.      (not (and (listp (symbol-function 'remove-directory))
  4916.            (eq (car (symbol-function 'remove-directory)) 'autoload))))
  4917.     (ange-ftp-overwrite-fn 'remove-directory))
  4918. ;; XEmacs change -- ange-ftp-diff is hosed
  4919. ;;(if (and (fboundp 'diff)
  4920. ;;     (not (and (listp (symbol-function 'diff))
  4921. ;;           (eq (car (symbol-function 'diff)) 'autoload))))
  4922. ;;      (ange-ftp-overwrite-fn 'diff))
  4923.  
  4924. (ange-ftp-overwrite-fn 'insert-file-contents)
  4925. (ange-ftp-overwrite-fn 'directory-files)
  4926. (ange-ftp-overwrite-fn 'file-directory-p)
  4927. (ange-ftp-overwrite-fn 'file-writable-p)
  4928. (ange-ftp-overwrite-fn 'file-readable-p)
  4929. (ange-ftp-overwrite-fn 'file-symlink-p)
  4930. (ange-ftp-overwrite-fn 'delete-file)
  4931. ;; (ange-ftp-overwrite-fn 'read-file-name-internal)
  4932. (ange-ftp-overwrite-fn 'verify-visited-file-modtime)
  4933. (ange-ftp-overwrite-fn 'file-exists-p)
  4934. (ange-ftp-overwrite-fn 'write-region)
  4935. (ange-ftp-overwrite-fn 'backup-buffer)
  4936. (ange-ftp-overwrite-fn 'copy-file)
  4937. (ange-ftp-overwrite-fn 'rename-file)
  4938. (ange-ftp-overwrite-fn 'file-attributes)
  4939. (ange-ftp-overwrite-fn 'file-name-directory)
  4940. (ange-ftp-overwrite-fn 'file-name-nondirectory)
  4941. (ange-ftp-overwrite-fn 'file-name-as-directory)
  4942. (ange-ftp-overwrite-fn 'directory-file-name)
  4943. (ange-ftp-overwrite-fn 'expand-file-name)
  4944. (ange-ftp-overwrite-fn 'file-name-all-completions)
  4945. (ange-ftp-overwrite-fn 'file-name-completion)
  4946.  
  4947. (or (memq 'ange-ftp-set-buffer-mode find-file-hooks)
  4948.     (setq find-file-hooks
  4949.       (cons 'ange-ftp-set-buffer-mode find-file-hooks)))
  4950.  
  4951.  
  4952. ;;;; ------------------------------------------------------------
  4953. ;;;; VOS support (VOS support is probably broken,
  4954. ;;;; but I don't know anything about VOS.)
  4955. ;;;; ------------------------------------------------------------
  4956. ;
  4957. ;(defun ange-ftp-fix-path-for-vos (path &optional reverse)
  4958. ;  (setq path (copy-sequence path))
  4959. ;  (let ((from (if reverse ?\> ?\/))
  4960. ;    (to (if reverse ?\/ ?\>))
  4961. ;    (i (1- (length path))))
  4962. ;    (while (>= i 0)
  4963. ;      (if (= (aref path i) from)
  4964. ;      (aset path i to))
  4965. ;      (setq i (1- i)))
  4966. ;    path))
  4967. ;
  4968. ;(or (assq 'vos ange-ftp-fix-path-func-alist)
  4969. ;    (setq ange-ftp-fix-path-func-alist
  4970. ;      (cons '(vos . ange-ftp-fix-path-for-vos)
  4971. ;        ange-ftp-fix-path-func-alist)))
  4972. ;
  4973. ;(or (memq 'vos ange-ftp-dumb-host-types)
  4974. ;    (setq ange-ftp-dumb-host-types
  4975. ;      (cons 'vos ange-ftp-dumb-host-types)))
  4976. ;
  4977. ;(defun ange-ftp-fix-dir-path-for-vos (dir-path)
  4978. ;  (ange-ftp-fix-path-for-vos
  4979. ;   (concat dir-path
  4980. ;       (if (eq ?/ (aref dir-path (1- (length dir-path))))
  4981. ;           "" "/")
  4982. ;       "*")))
  4983. ;
  4984. ;(or (assq 'vos ange-ftp-fix-dir-path-func-alist)
  4985. ;    (setq ange-ftp-fix-dir-path-func-alist
  4986. ;      (cons '(vos . ange-ftp-fix-dir-path-for-vos)
  4987. ;        ange-ftp-fix-dir-path-func-alist)))
  4988. ;
  4989. ;(defvar ange-ftp-vos-host-regexp nil
  4990. ;  "If a host matches this regexp then it is assumed to be running VOS.")
  4991. ;
  4992. ;(defun ange-ftp-vos-host (host)
  4993. ;  (and ange-ftp-vos-host-regexp
  4994. ;       (ange-ftp-save-match-data
  4995. ;     (string-match ange-ftp-vos-host-regexp host))))
  4996. ;
  4997. ;(defun ange-ftp-parse-vos-listing ()
  4998. ;  "Parse the current buffer which is assumed to be in VOS list -all
  4999. ;format, and return a hashtable as the result."
  5000. ;  (let ((tbl (ange-ftp-make-hashtable))
  5001. ;    (type-list
  5002. ;     '(("^Files: [0-9]+ +Blocks: [0-9]+\n+" nil 40)
  5003. ;       ("^Dirs: [0-9]+\n+" t 30)))
  5004. ;    type-regexp type-is-dir type-col file)
  5005. ;    (goto-char (point-min))
  5006. ;    (ange-ftp-save-match-data
  5007. ;      (while type-list
  5008. ;    (setq type-regexp (car (car type-list))
  5009. ;          type-is-dir (nth 1 (car type-list))
  5010. ;          type-col (nth 2 (car type-list))
  5011. ;          type-list (cdr type-list))
  5012. ;    (if (re-search-forward type-regexp nil t)
  5013. ;        (while (eq (char-after (point)) ? )
  5014. ;          (move-to-column type-col)
  5015. ;          (setq file (buffer-substring (point)
  5016. ;                       (progn
  5017. ;                         (end-of-line 1)
  5018. ;                         (point))))
  5019. ;          (ange-ftp-put-hash-entry file type-is-dir tbl)
  5020. ;          (forward-line 1))))
  5021. ;      (ange-ftp-put-hash-entry "." 'vosdir tbl)
  5022. ;      (ange-ftp-put-hash-entry ".." 'vosdir tbl))
  5023. ;    tbl))
  5024. ;
  5025. ;(or (assq 'vos ange-ftp-parse-list-func-alist)
  5026. ;    (setq ange-ftp-parse-list-func-alist
  5027. ;      (cons '(vos . ange-ftp-parse-vos-listing)
  5028. ;        ange-ftp-parse-list-func-alist)))
  5029.  
  5030. ;;;; ------------------------------------------------------------
  5031. ;;;; VMS support.
  5032. ;;;; ------------------------------------------------------------
  5033.  
  5034. (defun ange-ftp-fix-path-for-vms (path &optional reverse)
  5035.   "Convert PATH from UNIX-ish to VMS.  If REVERSE given then convert from VMS
  5036. to UNIX-ish."
  5037.   (ange-ftp-save-match-data
  5038.     (if reverse
  5039.     (if (string-match "^\\([^:]+:\\)?\\(\\[.*\\]\\)?\\([^][]*\\)$" path)
  5040.         (let (drive dir file)
  5041.           (if (match-beginning 1)
  5042.           (setq drive (substring path
  5043.                      (match-beginning 1)
  5044.                      (match-end 1))))
  5045.           (if (match-beginning 2)
  5046.           (setq dir
  5047.             (substring path (match-beginning 2) (match-end 2))))
  5048.           (if (match-beginning 3)
  5049.           (setq file
  5050.             (substring path (match-beginning 3) (match-end 3))))
  5051.           (and dir
  5052.            (setq dir (apply (function concat)
  5053.                     (mapcar (function
  5054.                          (lambda (char)
  5055.                            (if (= char ?.)
  5056.                            (vector ?/)
  5057.                          (vector char))))
  5058.                         (substring dir 1 -1)))))
  5059.           (concat (and drive
  5060.                (concat "/" drive "/"))
  5061.               dir (and dir "/")
  5062.               file))
  5063.       (error "path %s didn't match" path))
  5064.       (let (drive dir file tmp)
  5065.     (if (string-match "^/[^:]+:/" path)
  5066.         (setq drive (substring path 1
  5067.                    (1- (match-end 0)))
  5068.           path (substring path (match-end 0))))
  5069.     (setq tmp (file-name-directory path))
  5070.     (if tmp
  5071.         (setq dir (apply (function concat)
  5072.                  (mapcar (function
  5073.                       (lambda (char)
  5074.                     (if (= char ?/)
  5075.                         (vector ?.)
  5076.                       (vector char))))
  5077.                      (substring tmp 0 -1)))))
  5078.     (setq file (file-name-nondirectory path))
  5079.     (concat drive
  5080.         (and dir (concat "[" (if drive nil ".") dir "]"))
  5081.         file)))))
  5082.  
  5083. ;; (ange-ftp-fix-path-for-vms "/PUB$:/ANONYMOUS/SDSCPUB/NEXT/Readme.txt;1")
  5084. ;; (ange-ftp-fix-path-for-vms "/PUB$:[ANONYMOUS.SDSCPUB.NEXT]Readme.txt;1" t)
  5085.  
  5086. (or (assq 'vms ange-ftp-fix-path-func-alist)
  5087.     (setq ange-ftp-fix-path-func-alist
  5088.       (cons '(vms . ange-ftp-fix-path-for-vms)
  5089.         ange-ftp-fix-path-func-alist)))
  5090.  
  5091. (or (memq 'vms ange-ftp-dumb-host-types)
  5092.     (setq ange-ftp-dumb-host-types
  5093.       (cons 'vms ange-ftp-dumb-host-types)))
  5094.  
  5095. ;; It is important that this function barf for directories for which we know
  5096. ;; that we cannot possibly get a directory listing, such as "/" and "/DEV:/".
  5097. ;; This is because it saves an unnecessary FTP error, or possibly the listing
  5098. ;; might succeed, but give erroneous info. This last case is particularly
  5099. ;; likely for OS's (like MTS) for which we need to use a wildcard in order
  5100. ;; to list a directory.
  5101.  
  5102. (defun ange-ftp-fix-dir-path-for-vms (dir-path)
  5103.   "Convert path from UNIX-ish to VMS ready for a DIRectory listing."
  5104.   ;; Should there be entries for .. -> [-] and . -> [] below. Don't
  5105.   ;; think so, because expand-filename should have already short-circuited
  5106.   ;; them.
  5107.   (cond ((string-equal dir-path "/")
  5108.      (error "Cannot get listing for fictitious \"/\" directory."))
  5109.     ((string-match "^/[-A-Z0-9_$]+:/$" dir-path)
  5110.      (error "Cannot get listing for device."))
  5111.     ((ange-ftp-fix-path-for-vms dir-path))))
  5112.   
  5113. (or (assq 'vms ange-ftp-fix-dir-path-func-alist)
  5114.     (setq ange-ftp-fix-dir-path-func-alist
  5115.       (cons '(vms . ange-ftp-fix-dir-path-for-vms)
  5116.         ange-ftp-fix-dir-path-func-alist)))
  5117.  
  5118. (defvar ange-ftp-vms-host-regexp nil)
  5119.  
  5120. (defun ange-ftp-vms-host (host)
  5121.   "Return whether HOST is running VMS."
  5122.   (and ange-ftp-vms-host-regexp
  5123.        (ange-ftp-save-match-data
  5124.      (string-match ange-ftp-vms-host-regexp host))))
  5125.  
  5126. ;; Because some VMS ftp servers convert filenames to lower case
  5127. ;; we allow a-z in the filename regexp. I'm not too happy about this.
  5128.  
  5129. (defconst ange-ftp-vms-filename-regexp
  5130.   (concat
  5131.    "\\(\\([_A-Za-z0-9$]?\\|[_A-Za-z0-9$][_A-Za-z0-9$---]*\\)\\."
  5132.    "[_A-Za-z0-9$---]*;+[0-9]*\\)")
  5133.   "Regular expression to match for a valid VMS file name in Dired buffer.
  5134. Stupid freaking bug! Position of _ and $ shouldn't matter but they do.
  5135. Having [A-Z0-9$_] bombs on filename _$$CHANGE_LOG$.TXT$ and $CHANGE_LOG$.TX
  5136. Other orders of $ and _ seem to all work just fine.")
  5137.  
  5138. ;; These parsing functions are as general as possible because the syntax
  5139. ;; of ftp listings from VMS hosts is a bit erratic. What saves us is that
  5140. ;; the VMS filename syntax is so rigid. If they bomb on a listing in the
  5141. ;; standard VMS Multinet format, then this is a bug. If they bomb on a listing
  5142. ;; from vms.weird.net, then too bad.
  5143.  
  5144. (defun ange-ftp-parse-vms-filename ()
  5145.   "Extract the next filename from a VMS dired-like listing."
  5146.   (if (re-search-forward
  5147.      ange-ftp-vms-filename-regexp
  5148.      nil t)
  5149.     (buffer-substring (match-beginning 0) (match-end 0))))
  5150.  
  5151. (defun ange-ftp-parse-vms-listing ()
  5152.   "Parse the current buffer which is assumed to be in MultiNet FTP dir
  5153. format, and return a hashtable as the result."
  5154.   (let ((tbl (ange-ftp-make-hashtable))
  5155.     file)
  5156.     (goto-char (point-min))
  5157.     (ange-ftp-save-match-data
  5158.       (while (setq file (ange-ftp-parse-vms-filename))
  5159.     (if (string-match "\\.\\(DIR\\|dir\\);[0-9]+" file)
  5160.         ;; deal with directories
  5161.         (ange-ftp-put-hash-entry
  5162.          (substring file 0 (match-beginning 0)) t tbl)
  5163.       (ange-ftp-put-hash-entry file nil tbl)
  5164.       (if (string-match ";[0-9]+$" file) ; deal with extension
  5165.           ;; sans extension
  5166.           (ange-ftp-put-hash-entry
  5167.            (substring file 0 (match-beginning 0)) nil tbl)))
  5168.     (forward-line 1))
  5169.       ;; Would like to look for a "Total" line, or a "Directory" line to
  5170.       ;; make sure that the listing isn't complete garbage before putting
  5171.       ;; in "." and "..", but we can't even count on all VAX's giving us
  5172.       ;; either of these.
  5173.        (ange-ftp-put-hash-entry "." t tbl)
  5174.        (ange-ftp-put-hash-entry ".." t tbl))
  5175.     tbl))
  5176.  
  5177. (or (assq 'vms ange-ftp-parse-list-func-alist)
  5178.     (setq ange-ftp-parse-list-func-alist
  5179.       (cons '(vms . ange-ftp-parse-vms-listing)
  5180.         ange-ftp-parse-list-func-alist)))
  5181.  
  5182. ;; This version only deletes file entries which have
  5183. ;; explicit version numbers, because that is all VMS allows.
  5184.  
  5185. ;; Can the following two functions be speeded up using file
  5186. ;; completion functions?
  5187.  
  5188. (defun ange-ftp-vms-delete-file-entry (path &optional dir-p)
  5189.   (if dir-p
  5190.       (ange-ftp-internal-delete-file-entry path t)
  5191.     (ange-ftp-save-match-data
  5192.       (let ((file (ange-ftp-get-file-part path)))
  5193.     (if (string-match ";[0-9]+$" file)
  5194.         ;; In VMS you can't delete a file without an explicit    
  5195.         ;; version number, or wild-card (e.g. FOO;*)
  5196.         ;; For now, we give up on wildcards.
  5197.         (let ((files (ange-ftp-get-hash-entry
  5198.               (file-name-directory path)
  5199.               ange-ftp-files-hashtable)))
  5200.           (if files
  5201.           (let* ((root (substring file 0
  5202.                       (match-beginning 0)))
  5203.              (regexp (concat "^"
  5204.                      (regexp-quote root)
  5205.                      ";[0-9]+$"))
  5206.              versions)
  5207.             (ange-ftp-del-hash-entry file files)
  5208.             ;; Now we need to check if there are any
  5209.             ;; versions left. If not, then delete the
  5210.             ;; root entry.
  5211.             (mapatoms
  5212.              '(lambda (sym)
  5213.             (and (string-match regexp (get sym 'key))
  5214.                  (setq versions t)))
  5215.              files)
  5216.             (or versions
  5217.             (ange-ftp-del-hash-entry root files))))))))))
  5218.  
  5219. (or (assq 'vms ange-ftp-delete-file-entry-alist)
  5220.     (setq ange-ftp-delete-file-entry-alist
  5221.       (cons '(vms . ange-ftp-vms-delete-file-entry)
  5222.         ange-ftp-delete-file-entry-alist)))
  5223.  
  5224. (defun ange-ftp-vms-add-file-entry (path &optional dir-p)
  5225.   (if dir-p
  5226.       (ange-ftp-internal-add-file-entry path t)
  5227.     (let ((files (ange-ftp-get-hash-entry
  5228.           (file-name-directory path)
  5229.           ange-ftp-files-hashtable)))
  5230.       (if files
  5231.       (let ((file (ange-ftp-get-file-part path)))
  5232.         (ange-ftp-save-match-data
  5233.           (if (string-match ";[0-9]+$" file)
  5234.           (ange-ftp-put-hash-entry
  5235.            (substring file 0 (match-beginning 0))
  5236.            nil files)
  5237.         ;; Need to figure out what version of the file
  5238.         ;; is being added.
  5239.         (let ((regexp (concat "^"
  5240.                       (regexp-quote file)
  5241.                       ";\\([0-9]+\\)$"))
  5242.               (version 0))
  5243.           (mapatoms
  5244.            '(lambda (sym)
  5245.               (let ((name (get sym 'key)))
  5246.             (and (string-match regexp name)
  5247.                  (setq version
  5248.                    (max version
  5249.                     (string-to-int
  5250.                      (substring name
  5251.                             (match-beginning 1)
  5252.                             (match-end 1))))))))
  5253.            files)
  5254.           (setq version (1+ version))
  5255.           (ange-ftp-put-hash-entry
  5256.            (concat file ";" (int-to-string version))
  5257.            nil files))))
  5258.         (ange-ftp-put-hash-entry file nil files))))))
  5259.  
  5260. (or (assq 'vms ange-ftp-add-file-entry-alist)
  5261.     (setq ange-ftp-add-file-entry-alist
  5262.       (cons '(vms . ange-ftp-vms-add-file-entry)
  5263.         ange-ftp-add-file-entry-alist)))
  5264.  
  5265.  
  5266. (defun ange-ftp-add-vms-host (host)
  5267.   "Interactively adds a given HOST to ange-ftp-vms-host-regexp."
  5268.   (interactive
  5269.    (list (read-string "Host: "
  5270.               (let ((name (or (buffer-file-name)
  5271.                       (and (eq major-mode 'dired-mode)
  5272.                        dired-directory))))
  5273.             (and name (car (ange-ftp-ftp-path name)))))))
  5274.   (if (not (ange-ftp-vms-host host))
  5275.       (setq ange-ftp-vms-host-regexp
  5276.         (concat "^" (regexp-quote host) "$"
  5277.             (and ange-ftp-vms-host-regexp "\\|")
  5278.             ange-ftp-vms-host-regexp)
  5279.         ange-ftp-host-cache nil)))
  5280.  
  5281.  
  5282. (defun ange-ftp-vms-file-name-as-directory (name)
  5283.   (ange-ftp-save-match-data
  5284.     (if (string-match "\\.\\(DIR\\|dir\\)\\(;[0-9]+\\)?$" name)
  5285.     (setq name (substring name 0 (match-beginning 0))))
  5286.     (ange-ftp-real-file-name-as-directory name)))
  5287.  
  5288. (or (assq 'vms ange-ftp-file-name-as-directory-alist)
  5289.     (setq ange-ftp-file-name-as-directory-alist
  5290.       (cons '(vms . ange-ftp-vms-file-name-as-directory)
  5291.         ange-ftp-file-name-as-directory-alist)))
  5292.  
  5293. ;;; Tree dired support:
  5294.  
  5295. ;; For this code I have borrowed liberally from Sebastian Kremer's
  5296. ;; dired-vms.el
  5297.  
  5298.  
  5299. ;; These regexps must be anchored to beginning of line.
  5300. ;; Beware that the ftpd may put the device in front of the filename.
  5301.  
  5302. (defconst ange-ftp-dired-vms-re-exe "^. [^ \t.]+\\.\\(EXE\\|exe\\)[; ]"
  5303.   "Regular expression to use to search for VMS executable files.")
  5304.  
  5305. (defconst ange-ftp-dired-vms-re-dir "^. [^ \t.]+\\.\\(DIR\\|dir\\)[; ]"
  5306.   "Regular expression to use to search for VMS directories.")
  5307.  
  5308. (or (assq 'vms ange-ftp-dired-re-exe-alist)
  5309.     (setq ange-ftp-dired-re-exe-alist
  5310.       (cons (cons 'vms  ange-ftp-dired-vms-re-exe)
  5311.         ange-ftp-dired-re-exe-alist)))
  5312.  
  5313. (or (assq 'vms ange-ftp-dired-re-dir-alist)
  5314.     (setq ange-ftp-dired-re-dir-alist
  5315.       (cons (cons 'vms  ange-ftp-dired-vms-re-dir)
  5316.         ange-ftp-dired-re-dir-alist)))
  5317.  
  5318. (defun ange-ftp-dired-vms-insert-headerline (dir)
  5319.   ;; VMS inserts a headerline. I would prefer the headerline
  5320.   ;; to be in ange-ftp format. This version tries to
  5321.   ;; be careful, because we can't count on a headerline
  5322.   ;; over ftp, and we wouldn't want to delete anything
  5323.   ;; important.
  5324.   (save-excursion
  5325.     (if (looking-at "^  wildcard ")
  5326.     (forward-line 1))
  5327.     (if (looking-at "^[ \n\t]*[^\n]+\\][ \t]*\n")
  5328.     (delete-region (point) (match-end 0))))
  5329.   (ange-ftp-real-dired-insert-headerline dir))
  5330.  
  5331. (or (assq 'vms ange-ftp-dired-insert-headerline-alist)
  5332.     (setq ange-ftp-dired-insert-headerline-alist
  5333.       (cons '(vms . ange-ftp-dired-vms-insert-headerline)
  5334.         ange-ftp-dired-insert-headerline-alist)))
  5335.  
  5336. (defun ange-ftp-dired-vms-move-to-filename (&optional raise-error eol)
  5337.   "In dired, move to first char of filename on this line.
  5338. Returns position (point) or nil if no filename on this line."
  5339.   ;; This is the VMS version.
  5340.   (let (case-fold-search)
  5341.     (or eol (setq eol (progn (end-of-line) (point))))
  5342.     (beginning-of-line)
  5343.     (if (re-search-forward ange-ftp-vms-filename-regexp eol t)
  5344.     (goto-char (match-beginning 1))
  5345.       (if raise-error
  5346.       (error "No file on this line")
  5347.     nil))))
  5348.  
  5349. (or (assq 'vms ange-ftp-dired-move-to-filename-alist)
  5350.     (setq ange-ftp-dired-move-to-filename-alist
  5351.       (cons '(vms . ange-ftp-dired-vms-move-to-filename)
  5352.         ange-ftp-dired-move-to-filename-alist)))
  5353.  
  5354. (defun ange-ftp-dired-vms-move-to-end-of-filename (&optional no-error eol)
  5355.   ;; Assumes point is at beginning of filename.
  5356.   ;; So, it should be called only after (dired-move-to-filename t).
  5357.   ;; case-fold-search must be nil, at least for VMS.
  5358.   ;; On failure, signals an error or returns nil.
  5359.   ;; This is the VMS version.
  5360.   (let (opoint hidden case-fold-search)
  5361.     (setq opoint (point))
  5362.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  5363.     (setq hidden (and selective-display
  5364.               (save-excursion (search-forward "\r" eol t))))
  5365.     (if hidden
  5366.     nil
  5367.       (re-search-forward ange-ftp-vms-filename-regexp eol t))
  5368.     (or no-error
  5369.     (not (eq opoint (point)))
  5370.     (error
  5371.      (if hidden
  5372.          (substitute-command-keys
  5373.           "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5374.        "No file on this line")))
  5375.     (if (eq opoint (point))
  5376.     nil
  5377.       (point))))
  5378.  
  5379. (or (assq 'vms ange-ftp-dired-move-to-end-of-filename-alist)
  5380.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5381.       (cons '(vms . ange-ftp-dired-vms-move-to-end-of-filename)
  5382.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5383.  
  5384. (defun ange-ftp-dired-vms-between-files ()
  5385.   (save-excursion
  5386.     (beginning-of-line)
  5387.     (or (equal (following-char) 10) ; newline
  5388.      (equal (following-char) 9)     ; tab
  5389.      (progn (forward-char 2)
  5390.         (or (looking-at "Total of")
  5391.         (equal (following-char) 32))))))
  5392.  
  5393. (or (assq 'vms ange-ftp-dired-between-files-alist)
  5394.     (setq ange-ftp-dired-between-files-alist
  5395.       (cons '(vms . ange-ftp-dired-vms-between-files)
  5396.         ange-ftp-dired-between-files-alist)))
  5397.  
  5398. ;; Beware! In VMS filenames must be of the form "FILE.TYPE".
  5399. ;; Therefore, we cannot just append a ".Z" to filenames for
  5400. ;; compressed files. Instead, we turn "FILE.TYPE" into
  5401. ;; "FILE.TYPE-Z". Hope that this is a reasonable thing to do.
  5402.  
  5403. (defun ange-ftp-vms-make-compressed-filename (name &optional reverse)
  5404.   (if reverse
  5405.       (cond
  5406.        ((string-match "-Z;[0-9]+$" name)
  5407.     (substring name 0 (match-beginning 0)))
  5408.        ((string-match ";[0-9]+$" name)
  5409.     (substring name 0 (match-beginning 0)))
  5410.        ((string-match "-Z$" name)
  5411.     (substring name 0 -2))
  5412.        (t name))
  5413.     (if (string-match ";[0-9]+$" name)
  5414.     (concat (substring name 0 (match-beginning 0))
  5415.         "-Z")
  5416.       (concat name "-Z"))))
  5417.  
  5418. (or (assq 'vms ange-ftp-dired-compress-make-compressed-filename-alist)
  5419.     (setq ange-ftp-dired-compress-make-compressed-filename-alist
  5420.       (cons '(vms . ange-ftp-vms-make-compressed-filename)
  5421.         ange-ftp-dired-compress-make-compressed-filename-alist)))
  5422.  
  5423. ;; When the filename is too long, VMS will use two lines to list a file
  5424. ;; (damn them!) This will confuse dired. To solve this, need to convince
  5425. ;; Sebastian to use a function dired-go-to-end-of-file-line, instead of
  5426. ;; (forward-line 1). This would require a number of changes to dired.el.
  5427. ;; If dired gets confused, revert-buffer will fix it.
  5428.  
  5429. (defun ange-ftp-dired-vms-ls-trim ()
  5430.   (goto-char (point-min))
  5431.   (let ((case-fold-search nil))
  5432.     (re-search-forward  ange-ftp-vms-filename-regexp))
  5433.   (beginning-of-line)
  5434.   (delete-region (point-min) (point))
  5435.   (forward-line 1)
  5436.   (delete-region (point) (point-max)))
  5437.  
  5438.  
  5439. (or (assq 'vms ange-ftp-dired-ls-trim-alist)
  5440.     (setq ange-ftp-dired-ls-trim-alist
  5441.       (cons '(vms . ange-ftp-dired-vms-ls-trim)
  5442.         ange-ftp-dired-ls-trim-alist)))    
  5443.  
  5444. (defun ange-ftp-vms-bob-version (name)
  5445.   (ange-ftp-save-match-data
  5446.     (if (string-match ";[0-9]+$" name)
  5447.     (substring name 0 (match-beginning 0))
  5448.       name)))
  5449.  
  5450. (or (assq 'vms ange-ftp-bob-version-alist)
  5451.     (setq ange-ftp-bob-version-alist
  5452.       (cons '(vms . ange-ftp-vms-bob-version)
  5453.         ange-ftp-bob-version-alist)))
  5454.  
  5455. ;;; The vms version of clean-directory has 2 more optional args
  5456. ;;; than the usual dired version. This is so that it can be used by
  5457. ;;; ange-ftp-dired-vms-flag-backup-files.
  5458.  
  5459. (defun ange-ftp-dired-vms-clean-directory (keep &optional marker msg)
  5460.   "Flag numerical backups for deletion.
  5461. Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
  5462. Positive prefix arg KEEP overrides `dired-kept-versions';
  5463. Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
  5464.  
  5465. To clear the flags on these files, you can use \\[dired-flag-backup-files]
  5466. with a prefix argument."
  5467. ;  (interactive "P") ; Never actually called interactively.
  5468.   (setq keep (max 1 (if keep (prefix-numeric-value keep) dired-kept-versions)))
  5469.   (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
  5470.     ;; late-retention must NEVER be allowed to be less than 1 in VMS!
  5471.     ;; This could wipe ALL copies of the file.
  5472.     (late-retention (max 1 (if (<= keep 0) dired-kept-versions keep)))
  5473.     (action (or msg "Cleaning"))
  5474.     (trample-marker (or marker dired-del-marker))
  5475.     (file-version-assoc-list ()))
  5476.     (message (concat action
  5477.              " numerical backups (keeping %d late, %d old)...")
  5478.          late-retention early-retention)
  5479.     ;; Look at each file.
  5480.     ;; If the file has numeric backup versions,
  5481.     ;; put on file-version-assoc-list an element of the form
  5482.     ;; (FILENAME . VERSION-NUMBER-LIST)
  5483.     (dired-map-dired-file-lines (function
  5484.                  ange-ftp-dired-vms-collect-file-versions))
  5485.     ;; Sort each VERSION-NUMBER-LIST,
  5486.     ;; and remove the versions not to be deleted.
  5487.     (let ((fval file-version-assoc-list))
  5488.       (while fval
  5489.     (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
  5490.            (v-count (length sorted-v-list)))
  5491.       (if (> v-count (+ early-retention late-retention))
  5492.           (rplacd (nthcdr early-retention sorted-v-list)
  5493.               (nthcdr (- v-count late-retention)
  5494.                   sorted-v-list)))
  5495.       (rplacd (car fval)
  5496.           (cdr sorted-v-list)))
  5497.     (setq fval (cdr fval))))
  5498.     ;; Look at each file.  If it is a numeric backup file,
  5499.     ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
  5500.     (dired-map-dired-file-lines
  5501.      (function
  5502.       ange-ftp-dired-vms-trample-file-versions mark))
  5503.     (message (concat action " numerical backups...done"))))
  5504.  
  5505. (or (assq 'vms ange-ftp-dired-clean-directory-alist)
  5506.     (setq ange-ftp-dired-clean-directory-alist
  5507.       (cons '(vms . ange-ftp-dired-vms-clean-directory)
  5508.         ange-ftp-dired-clean-directory-alist)))
  5509.  
  5510. (defun ange-ftp-dired-vms-collect-file-versions (fn)
  5511.   ;;  "If it looks like file FN has versions, return a list of the versions.
  5512.   ;;That is a list of strings which are file names.
  5513.   ;;The caller may want to flag some of these files for deletion."
  5514. (let ((path (nth 2 (ange-ftp-ftp-path fn))))
  5515.   (if (string-match ";[0-9]+$" path)
  5516.       (let* ((path (substring path 0 (match-beginning 0)))
  5517.          (fn (ange-ftp-replace-path-component fn path)))
  5518.     (if (not (assq fn file-version-assoc-list))
  5519.         (let* ((base-versions
  5520.             (concat (file-name-nondirectory path) ";"))
  5521.            (bv-length (length base-versions))
  5522.            (possibilities (file-name-all-completions
  5523.                    base-versions
  5524.                    (file-name-directory fn)))
  5525.            (versions (mapcar
  5526.                   '(lambda (arg)
  5527.                  (if (and (string-match
  5528.                        "[0-9]+$" arg bv-length)
  5529.                       (= (match-beginning 0) bv-length))
  5530.                      (string-to-int (substring arg bv-length))
  5531.                    0))
  5532.                   possibilities)))
  5533.           (if versions
  5534.           (setq
  5535.            file-version-assoc-list
  5536.            (cons (cons fn versions)
  5537.              file-version-assoc-list)))))))))
  5538.  
  5539. (defun ange-ftp-dired-vms-trample-file-versions (fn)
  5540.   (let* ((start-vn (string-match ";[0-9]+$" fn))
  5541.      base-version-list)
  5542.     (and start-vn
  5543.      (setq base-version-list    ; there was a base version to which
  5544.            (assoc (substring fn 0 start-vn)    ; this looks like a
  5545.               file-version-assoc-list))    ; subversion
  5546.      (not (memq (string-to-int (substring fn (1+ start-vn)))
  5547.             base-version-list))    ; this one doesn't make the cut
  5548.      (progn (beginning-of-line)
  5549.         (delete-char 1)
  5550.         (insert trample-marker)))))
  5551.  
  5552. (defun ange-ftp-dired-vms-flag-backup-files (&optional unflag-p)
  5553.   (let ((dired-kept-versions 1)
  5554.     (kept-old-versions 0)
  5555.     marker msg)
  5556.     (if unflag-p
  5557.     (setq marker ?\040 msg "Unflagging")
  5558.       (setq marker dired-del-marker msg "Cleaning"))
  5559.     (ange-ftp-dired-vms-clean-directory nil marker msg)))
  5560.  
  5561. (or (assq 'vms ange-ftp-dired-flag-backup-files-alist)
  5562.     (setq ange-ftp-dired-flag-backup-files-alist
  5563.       (cons '(vms . ange-ftp-dired-vms-flag-backup-files)
  5564.         ange-ftp-dired-flag-backup-files-alist)))
  5565.  
  5566. (defun ange-ftp-dired-vms-backup-diff (&optional switches)
  5567.   (let ((file (dired-get-filename 'no-dir))
  5568.     bak)
  5569.     (if (and (string-match ";[0-9]+$" file)
  5570.          ;; Find most recent previous version.
  5571.          (let ((root (substring file 0 (match-beginning 0)))
  5572.            (ver
  5573.             (string-to-int (substring file (1+ (match-beginning 0)))))
  5574.            found)
  5575.            (setq ver (1- ver))
  5576.            (while (and (> ver 0) (not found))
  5577.          (setq bak (concat root ";" (int-to-string ver)))
  5578.          (and (file-exists-p bak) (setq found t))
  5579.          (setq ver (1- ver)))
  5580.            found))
  5581.     (if switches
  5582.         (diff (expand-file-name bak) (expand-file-name file) switches)
  5583.       (diff (expand-file-name bak) (expand-file-name file)))
  5584.       (error "No previous version found for %s" file))))
  5585.  
  5586. (or (assq 'vms ange-ftp-dired-backup-diff-alist)
  5587.     (setq ange-ftp-dired-backup-diff-alist
  5588.       (cons '(vms . ange-ftp-dired-vms-backup-diff)
  5589.         ange-ftp-dired-backup-diff-alist)))
  5590.  
  5591.  
  5592. ;;;; ------------------------------------------------------------
  5593. ;;;; MTS support
  5594. ;;;; ------------------------------------------------------------
  5595.  
  5596.  
  5597. (defun ange-ftp-fix-path-for-mts (path &optional reverse)
  5598.   "Convert PATH from UNIX-ish to MTS. If REVERSE given then convert from
  5599. MTS to UNIX-ish."
  5600.   (ange-ftp-save-match-data
  5601.     (if reverse
  5602.     (if (string-match "^\\([^:]+:\\)?\\(.*\\)$" path)
  5603.         (let (acct file)
  5604.           (if (match-beginning 1)
  5605.           (setq acct (substring path 0 (match-end 1))))
  5606.           (if (match-beginning 2)
  5607.           (setq file (substring path
  5608.                     (match-beginning 2) (match-end 2))))
  5609.           (concat (and acct (concat "/" acct "/"))
  5610.               file))
  5611.       (error "path %s didn't match" path))
  5612.       (if (string-match "^/\\([^:]+:\\)/\\(.*\\)$" path)
  5613.       (concat (substring path 1 (match-end 1))
  5614.           (substring path (match-beginning 2) (match-end 2)))
  5615.     ;; Let's hope that mts will recognize it anyway.
  5616.     path))))
  5617.  
  5618. (or (assq 'mts ange-ftp-fix-path-func-alist)
  5619.     (setq ange-ftp-fix-path-func-alist
  5620.       (cons '(mts . ange-ftp-fix-path-for-mts)
  5621.         ange-ftp-fix-path-func-alist)))
  5622.  
  5623. (defun ange-ftp-fix-dir-path-for-mts (dir-path)
  5624.   "Convert path from UNIX-ish to MTS ready for a DIRectory listing.
  5625. Remember that there are no directories in MTS."
  5626.   (if (string-equal dir-path "/")
  5627.       (error "Cannot get listing for fictitious \"/\" directory.")
  5628.     (let ((dir-path (ange-ftp-fix-path-for-mts dir-path)))
  5629.       (cond
  5630.        ((string-equal dir-path "")
  5631.     "?")
  5632.        ((string-match ":$" dir-path)
  5633.     (concat dir-path "?"))
  5634.        (dir-path))))) ; It's just a single file.
  5635.  
  5636. (or (assq 'mts ange-ftp-fix-dir-path-func-alist)
  5637.     (setq ange-ftp-fix-dir-path-func-alist
  5638.       (cons '(mts . ange-ftp-fix-dir-path-for-mts)
  5639.         ange-ftp-fix-dir-path-func-alist)))
  5640.  
  5641. (or (memq 'mts ange-ftp-dumb-host-types)
  5642.     (setq ange-ftp-dumb-host-types
  5643.       (cons 'mts ange-ftp-dumb-host-types)))
  5644.  
  5645. (defvar ange-ftp-mts-host-regexp nil)
  5646.  
  5647. (defun ange-ftp-mts-host (host)
  5648.   "Return whether HOST is running MTS."
  5649.   (and ange-ftp-mts-host-regexp
  5650.        (ange-ftp-save-match-data
  5651.      (string-match ange-ftp-mts-host-regexp host))))
  5652.  
  5653. (defun ange-ftp-parse-mts-listing ()
  5654.   "Parse the current buffer which is assumed to be in
  5655. mts ftp dir format."
  5656.   (let ((tbl (ange-ftp-make-hashtable)))
  5657.     (goto-char (point-min))
  5658.     (ange-ftp-save-match-data
  5659.       (while (re-search-forward ange-ftp-date-regexp nil t)
  5660.     (end-of-line)
  5661.     (skip-chars-backward " ")
  5662.     (let ((end (point)))
  5663.       (skip-chars-backward "-A-Z0-9_.!")
  5664.       (ange-ftp-put-hash-entry (buffer-substring (point) end) nil tbl))
  5665.     (forward-line 1)))
  5666.       ;; Don't need to bother with ..
  5667.     (ange-ftp-put-hash-entry "." t tbl)
  5668.     tbl))
  5669.  
  5670. (or (assq 'mts ange-ftp-parse-list-func-alist)
  5671.     (setq ange-ftp-parse-list-func-alist
  5672.       (cons '(mts . ange-ftp-parse-mts-listing)
  5673.         ange-ftp-parse-list-func-alist)))
  5674.  
  5675. (defun ange-ftp-add-mts-host (host)
  5676.   "Interactively adds a given HOST to ange-ftp-mts-host-regexp."
  5677.   (interactive
  5678.    (list (read-string "Host: "
  5679.               (let ((name (or (buffer-file-name)
  5680.                       (and (eq major-mode 'dired-mode)
  5681.                        dired-directory))))
  5682.             (and name (car (ange-ftp-ftp-path name)))))))
  5683.   (if (not (ange-ftp-mts-host host))
  5684.       (setq ange-ftp-mts-host-regexp
  5685.         (concat "^" (regexp-quote host) "$"
  5686.             (and ange-ftp-mts-host-regexp "\\|")
  5687.             ange-ftp-mts-host-regexp)
  5688.         ange-ftp-host-cache nil)))
  5689.  
  5690. ;;; Tree dired support:
  5691.  
  5692. ;; There aren't too many systems left that use MTS. This dired support will
  5693. ;; work for the implementation of ftp on mtsg.ubc.ca. I hope other mts systems
  5694. ;; implement ftp in the same way. If not, it might be necessary to make the
  5695. ;; following more flexible.
  5696.  
  5697. (defun ange-ftp-dired-mts-move-to-filename (&optional raise-error eol)
  5698.   "In dired, move to first char of filename on this line.
  5699. Returns position (point) or nil if no filename on this line."
  5700.   ;; This is the MTS version.
  5701.   (or eol (setq eol (progn (end-of-line) (point))))
  5702.   (beginning-of-line)
  5703.   (if (re-search-forward
  5704.        ange-ftp-date-regexp eol t)
  5705.       (progn
  5706.     (skip-chars-forward " ")          ; Eat blanks after date
  5707.     (skip-chars-forward "0-9:" eol)   ; Eat time or year
  5708.     (skip-chars-forward " " eol)      ; one space before filename
  5709.     ;; When listing an account other than the users own account it appends
  5710.     ;; ACCT: to the beginning of the filename. Skip over this.
  5711.     (and (looking-at "[A-Z0-9_.]+:")
  5712.          (goto-char (match-end 0)))
  5713.     (point))
  5714.     (if raise-error
  5715.     (error "No file on this line")
  5716.       nil)))
  5717.  
  5718. (or (assq 'mts ange-ftp-dired-move-to-filename-alist)
  5719.     (setq ange-ftp-dired-move-to-filename-alist
  5720.       (cons '(mts . ange-ftp-dired-mts-move-to-filename)
  5721.         ange-ftp-dired-move-to-filename-alist)))
  5722.  
  5723. (defun ange-ftp-dired-mts-move-to-end-of-filename (&optional no-error eol)
  5724.   ;; Assumes point is at beginning of filename.
  5725.   ;; So, it should be called only after (dired-move-to-filename t).
  5726.   ;; On failure, signals an error or returns nil.
  5727.   ;; This is the MTS version.
  5728.   (let (opoint hidden case-fold-search)
  5729.     (setq opoint (point)
  5730.       eol (save-excursion (end-of-line) (point))
  5731.       hidden (and selective-display
  5732.               (save-excursion (search-forward "\r" eol t))))
  5733.     (if hidden
  5734.     nil
  5735.       (skip-chars-forward "-A-Z0-9._!" eol))
  5736.     (or no-error
  5737.     (not (eq opoint (point)))
  5738.     (error
  5739.      (if hidden
  5740.          (substitute-command-keys
  5741.           "File line is hidden, type \\[dired-hide-subdir] to unhide")
  5742.        "No file on this line")))
  5743.     (if (eq opoint (point))
  5744.     nil
  5745.       (point))))
  5746.  
  5747. (or (assq 'mts ange-ftp-dired-move-to-end-of-filename-alist)
  5748.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5749.       (cons '(mts . ange-ftp-dired-mts-move-to-end-of-filename)
  5750.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5751.  
  5752. ;;;; ------------------------------------------------------------
  5753. ;;;; CMS support
  5754. ;;;; ------------------------------------------------------------
  5755.  
  5756. ;; Since CMS doesn't have any full pathname syntax, we have to fudge
  5757. ;; things with cd's. We actually send too many cd's, but is dangerous
  5758. ;; to try to remember the current minidisk, because if the connection
  5759. ;; is closed and needs to be reopened, we will find ourselves back in
  5760. ;; the default minidisk. This is fairly likely since CMS ftp servers
  5761. ;; usually close the connection after 5 minutes of inactivity.
  5762.  
  5763. ;; Have I got the filename character set right?
  5764.  
  5765. (defun ange-ftp-fix-path-for-cms (path &optional reverse)
  5766.   "Convert PATH from UNIX-ish to CMS. If REVERSE is given, convert
  5767. from CMS to UNIX. Actually, CMS doesn't have a full pathname syntax,
  5768. so we fudge things by sending cd's."
  5769.   (ange-ftp-save-match-data
  5770.     (if reverse
  5771.     ;; Since we only convert output from a pwd in this direction,
  5772.     ;; we'll assume that it's a minidisk, and make it into a
  5773.     ;; directory file name. Note that the expand-dir-hashtable
  5774.     ;; stores directories without the trailing /. Is this
  5775.     ;; consistent?
  5776.     (concat "/" path)
  5777.       (if (string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$"
  5778.             path)
  5779.       (let ((minidisk (substring path 1 (match-end 1))))
  5780.         (if (match-beginning 2)
  5781.         (let ((file (substring path (match-beginning 2)
  5782.                        (match-end 2)))
  5783.               (cmd (concat "cd " minidisk))
  5784.  
  5785.               ;; Note that host and user are bound in the call
  5786.               ;; to ange-ftp-send-cmd
  5787.               (proc (ange-ftp-get-process host user)))
  5788.  
  5789.           ;; Must use ange-ftp-raw-send-cmd here to avoid
  5790.           ;; an infinite loop.
  5791.           (if (car (ange-ftp-raw-send-cmd proc cmd msg))
  5792.               file
  5793.             ;; failed... try ONCE more.
  5794.             (setq proc (ange-ftp-get-process host user))
  5795.             (let ((result (ange-ftp-raw-send-cmd proc cmd msg)))
  5796.               (if (car result)
  5797.               file
  5798.             ;; failed.  give up.
  5799.             (ange-ftp-error host user
  5800.                     (format "cd to minidisk %s failed: %s"
  5801.                         minidisk (cdr result)))))))
  5802.           ;; return the minidisk
  5803.           minidisk))
  5804.     (error "Invalid CMS filename")))))
  5805.  
  5806. (or (assq 'cms ange-ftp-fix-path-func-alist)
  5807.     (setq ange-ftp-fix-path-func-alist
  5808.       (cons '(cms . ange-ftp-fix-path-for-cms)
  5809.         ange-ftp-fix-path-func-alist)))
  5810.  
  5811. (or (memq 'cms ange-ftp-dumb-host-types)
  5812.     (setq ange-ftp-dumb-host-types
  5813.       (cons 'cms ange-ftp-dumb-host-types)))
  5814.  
  5815. (defun ange-ftp-fix-dir-path-for-cms (dir-path)
  5816.   "Convert path from UNIX-ish to VMS ready for a DIRectory listing."
  5817.   (cond
  5818.    ((string-equal "/" dir-path)
  5819.     (error "Cannot get listing for fictitious \"/\" directory."))
  5820.    ((string-match "^/\\([-A-Z0-9$*._]+\\)/\\([-A-Z0-9$._]+\\)?$" dir-path)
  5821.     (let* ((minidisk (substring dir-path (match-beginning 1) (match-end 1)))
  5822.        ;; host and user are bound in the call to ange-ftp-send-cmd
  5823.        (proc (ange-ftp-get-process host user))
  5824.        (cmd (concat "cd " minidisk))
  5825.        (file (if (match-beginning 2)
  5826.              ;; it's a single file
  5827.              (substring path (match-beginning 2)
  5828.                 (match-end 2))
  5829.            ;; use the wild-card
  5830.            "*")))
  5831.       (if (car (ange-ftp-raw-send-cmd proc cmd))
  5832.       file
  5833.     ;; try again...
  5834.     (setq proc (ange-ftp-get-process host user))
  5835.     (let ((result (ange-ftp-raw-send-cmd proc cmd)))
  5836.       (if (car result)
  5837.           file
  5838.         ;; give up
  5839.         (ange-ftp-error host user
  5840.                 (format "cd to minidisk %s failed: "
  5841.                     minidisk (cdr result))))))))
  5842.    (t (error "Invalid CMS pathname"))))
  5843.  
  5844. (or (assq 'cms ange-ftp-fix-dir-path-func-alist)
  5845.     (setq ange-ftp-fix-dir-path-func-alist
  5846.       (cons '(cms . ange-ftp-fix-dir-path-for-cms)
  5847.         ange-ftp-fix-dir-path-func-alist)))
  5848.  
  5849. (defvar ange-ftp-cms-host-regexp nil
  5850.   "Regular expression to match hosts running the CMS operating system.")
  5851.  
  5852. (defun ange-ftp-cms-host (host)
  5853.   "Return whether the host is running CMS."
  5854.   (and ange-ftp-cms-host-regexp
  5855.        (ange-ftp-save-match-data
  5856.      (string-match ange-ftp-cms-host-regexp host))))
  5857.  
  5858. (defun ange-ftp-add-cms-host (host)
  5859.   "Interactively adds a given HOST to ange-ftp-cms-host-regexp."
  5860.   (interactive
  5861.    (list (read-string "Host: "
  5862.               (let ((name (or (buffer-file-name)
  5863.                       (and (eq major-mode 'dired-mode)
  5864.                        dired-directory))))
  5865.             (and name (car (ange-ftp-ftp-path name)))))))
  5866.   (if (not (ange-ftp-cms-host host))
  5867.       (setq ange-ftp-cms-host-regexp
  5868.         (concat "^" (regexp-quote host) "$"
  5869.             (and ange-ftp-cms-host-regexp "\\|")
  5870.             ange-ftp-cms-host-regexp)
  5871.         ange-ftp-host-cache nil)))
  5872.  
  5873. (defun ange-ftp-parse-cms-listing ()
  5874.   "Parse the current buffer which is assumed to be a CMS directory listing."
  5875.   ;; If we succeed in getting a listing, then we will assume that the minidisk
  5876.   ;; exists. file is bound by the call to ange-ftp-ls. This doesn't work
  5877.   ;; because ange-ftp doesn't know that the root hashtable has only part of
  5878.   ;; the info. It will assume that if a minidisk isn't in it, then it doesn't
  5879.   ;; exist. It would be nice if completion worked for minidisks, as we
  5880.   ;; discover them.
  5881. ;  (let* ((dir-file (directory-file-name file))
  5882. ;     (root (file-name-directory dir-file))
  5883. ;     (minidisk (ange-ftp-get-file-part dir-file))
  5884. ;     (root-tbl (ange-ftp-get-hash-entry root ange-ftp-files-hashtable)))
  5885. ;    (if root-tbl
  5886. ;    (ange-ftp-put-hash-entry minidisk t root-tbl)
  5887. ;      (setq root-tbl (ange-ftp-make-hashtable))
  5888. ;      (ange-ftp-put-hash-entry minidisk t root-tbl)
  5889. ;      (ange-ftp-put-hash-entry "." t root-tbl)
  5890. ;      (ange-ftp-set-files root root-tbl)))
  5891.   ;; Now do the usual parsing
  5892.   (let ((tbl (ange-ftp-make-hashtable)))
  5893.     (goto-char (point-min))
  5894.     (ange-ftp-save-match-data
  5895.       (while
  5896.       (re-search-forward
  5897.        "^\\([-A-Z0-9$_]+\\) +\\([-A-Z0-9$_]+\\) +[VF] +[0-9]+ " nil t)
  5898.     (ange-ftp-put-hash-entry
  5899.      (concat (buffer-substring (match-beginning 1)
  5900.                    (match-end 1))
  5901.          "."
  5902.          (buffer-substring (match-beginning 2)
  5903.                    (match-end 2)))
  5904.      nil tbl)
  5905.     (forward-line 1))
  5906.       (ange-ftp-put-hash-entry "." t tbl))
  5907.     tbl))
  5908.  
  5909. (or (assq 'cms ange-ftp-parse-list-func-alist)
  5910.     (setq ange-ftp-parse-list-func-alist
  5911.       (cons '(cms . ange-ftp-parse-cms-listing)
  5912.         ange-ftp-parse-list-func-alist)))
  5913.     
  5914. ;;; Tree dired support:
  5915.  
  5916. (defconst ange-ftp-dired-cms-re-exe
  5917.   "^. [-A-Z0-9$_]+ +EXEC "
  5918.   "Regular expression to use to search for CMS executables.")
  5919.  
  5920. (or (assq 'cms ange-ftp-dired-re-exe-alist)
  5921.     (setq ange-ftp-dired-re-exe-alist
  5922.       (cons (cons 'cms  ange-ftp-dired-cms-re-exe)
  5923.         ange-ftp-dired-re-exe-alist)))
  5924.  
  5925.  
  5926. (defun ange-ftp-dired-cms-insert-headerline (dir)
  5927.   ;; CMS has no total line, so we insert a blank line for
  5928.   ;; aesthetics.
  5929.   (insert "\n")
  5930.   (forward-char -1)
  5931.   (ange-ftp-real-dired-insert-headerline dir))
  5932.  
  5933. (or (assq 'cms ange-ftp-dired-insert-headerline-alist)
  5934.     (setq ange-ftp-dired-insert-headerline-alist
  5935.       (cons '(cms . ange-ftp-dired-cms-insert-headerline)
  5936.         ange-ftp-dired-insert-headerline-alist)))
  5937.  
  5938. (defun ange-ftp-dired-cms-move-to-filename (&optional raise-error eol)
  5939.   "In dired, move to the first char of filename on this line."
  5940.   ;; This is the CMS version.
  5941.   (or eol (setq eol (progn (end-of-line) (point))))
  5942.   (let (case-fold-search)
  5943.     (beginning-of-line)
  5944.     (if (re-search-forward " [-A-Z0-9$_]+ +[-A-Z0-9$_]+ +[VF] +[0-9]+ " eol t)
  5945.     (goto-char (1+ (match-beginning 0)))
  5946.       (if raise-error
  5947.       (error "No file on this line")
  5948.     nil))))
  5949.  
  5950. (or (assq 'cms ange-ftp-dired-move-to-filename-alist)
  5951.     (setq ange-ftp-dired-move-to-filename-alist
  5952.       (cons '(cms . ange-ftp-dired-cms-move-to-filename)
  5953.         ange-ftp-dired-move-to-filename-alist)))
  5954.  
  5955. (defun ange-ftp-dired-cms-move-to-end-of-filename (&optional no-error eol)
  5956.   ;; Assumes point is at beginning of filename.
  5957.   ;; So, it should be called only after (dired-move-to-filename t).
  5958.   ;; case-fold-search must be nil, at least for VMS.
  5959.   ;; On failure, signals an error or returns nil.
  5960.   ;; This is the CMS version.
  5961.   (let ((opoint (point))
  5962.     case-fold-search hidden)
  5963.     (or eol (setq eol (save-excursion (end-of-line) (point))))
  5964.     (setq hidden (and selective-display
  5965.               (save-excursion
  5966.             (search-forward "\r" eol t))))
  5967.     (if hidden
  5968.     (if no-error
  5969.         nil
  5970.       (error
  5971.        (substitute-command-keys
  5972.         "File line is hidden, type \\[dired-hide-subdir] to unhide")))
  5973.       (skip-chars-forward "-A-Z0-9$_" eol)
  5974.       (skip-chars-forward " " eol)
  5975.       (skip-chars-forward "-A-Z0-9$_" eol)
  5976.       (if (eq opoint (point))
  5977.       (if no-error
  5978.           nil
  5979.         (error "No file on this line"))
  5980.     (point)))))
  5981.  
  5982. (or (assq 'cms ange-ftp-dired-move-to-end-of-filename-alist)
  5983.     (setq ange-ftp-dired-move-to-end-of-filename-alist
  5984.       (cons '(cms . ange-ftp-dired-cms-move-to-end-of-filename)
  5985.         ange-ftp-dired-move-to-end-of-filename-alist)))
  5986.  
  5987. (defun ange-ftp-cms-make-compressed-filename (name &optional reverse)
  5988.   (if reverse
  5989.       (if (string-match "-Z$" name)
  5990.       (substring name 0 -2)
  5991.     name)
  5992.     (concat name "-Z")))
  5993.  
  5994. (or (assq 'cms ange-ftp-dired-compress-make-compressed-filename-alist)
  5995.     (setq ange-ftp-dired-compress-make-compressed-filename-alist
  5996.       (cons '(cms . ange-ftp-cms-make-compressed-filename)
  5997.         ange-ftp-dired-compress-make-compressed-filename-alist)))
  5998.  
  5999. (defun ange-ftp-dired-cms-get-filename (&optional localp no-error-if-not-filep)
  6000.   (let ((name (ange-ftp-real-dired-get-filename localp no-error-if-not-filep)))
  6001.     (and name
  6002.      (if (string-match "^\\([^ ]+\\) +\\([^ ]+\\)$" name)
  6003.          (concat (substring name 0 (match-end 1))
  6004.              "."
  6005.              (substring name (match-beginning 2) (match-end 2)))
  6006.        name))))
  6007.  
  6008. (or (assq 'cms ange-ftp-dired-get-filename-alist)
  6009.     (setq ange-ftp-dired-get-filename-alist
  6010.       (cons '(cms . ange-ftp-dired-cms-get-filename)
  6011.         ange-ftp-dired-get-filename-alist)))
  6012.  
  6013. ;;;; ------------------------------------------------------------
  6014. ;;;; Finally provide package.
  6015. ;;;; ------------------------------------------------------------
  6016.  
  6017. ;; This is so that VC doesn't need to be hacked up.  I think the fsf way is
  6018. ;; a bit cleaner.  (Forgive me, as I have sinned...)  The great side-effect 
  6019. ;; of this change is that ange-ftp will now autoload...even w/o being fully
  6020. ;; converted to use the filename-handler-alist.  --Stig
  6021.  
  6022. ;; Turn off RCS/SCCS processing to save time.
  6023. ;; This returns nil for any file name as argument.
  6024. (put 'vc-registered 'ange-ftp 'null)
  6025. ^L
  6026. ;;; Define ways of getting at unmodified Emacs primitives,
  6027. ;;; turning off our handler.
  6028.  
  6029. (defun ange-ftp-run-real-handler (operation args)
  6030.   (let ((inhibit-file-name-handlers
  6031.          (cons 'ange-ftp-hook-function
  6032.                (cons 'ange-ftp-completion-hook-function
  6033.                      (and (eq inhibit-file-name-operation operation)
  6034.                           inhibit-file-name-handlers))))
  6035.         (inhibit-file-name-operation operation))
  6036.     (apply operation args)))
  6037.  
  6038. ;;;###autoload
  6039. (defun ange-ftp-hook-function (operation &rest args)
  6040.   (let ((fn (get operation 'ange-ftp)))
  6041.     (if fn (apply fn args)
  6042.       (ange-ftp-run-real-handler operation args))))
  6043.  
  6044. ;;;###autoload
  6045. (or (assoc (car ange-ftp-path-format) file-name-handler-alist)
  6046.     (setq file-name-handler-alist
  6047.           (cons (cons (car ange-ftp-path-format) 'ange-ftp-hook-function)
  6048.                 file-name-handler-alist)))
  6049.  
  6050. ;; ;;; This regexp recognizes and absolute filenames with only one component,
  6051. ;; ;;; for the sake of hostname completion.
  6052. ;; ;;;###autoload
  6053. ;; (or (assoc "^/[^/:]*\\'" file-name-handler-alist)
  6054. ;;     (setq file-name-handler-alist
  6055. ;;           (cons '("^/[^/:]*\\'" . ange-ftp-completion-hook-function)
  6056. ;;                 file-name-handler-alist)))
  6057.  
  6058. (provide 'ange-ftp)
  6059.